-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Problem 027 - Quadratic Primes algorithm and tests
- Loading branch information
1 parent
2b739bf
commit fd1d6e6
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Problem - Quadratic Primes | ||
* | ||
* @see {@link https://projecteuler.net/problem=27} | ||
* | ||
* The quadratic expression n^2 + an + b, where |a| < 1000 and |b| ≤ 1000, | ||
* produces a positive prime for consecutive values of n, starting with n = 0. | ||
* Find the product of the coefficients, a and b, for the quadratic expression that | ||
* produces the maximum number of primes for consecutive values of n. | ||
*/ | ||
|
||
/** | ||
* Main function to find the coefficients a and b that produce the maximum number | ||
* of consecutive primes for the quadratic expression n^2 + an + b. | ||
* | ||
* @returns {{maxPrimes: number, product: number}} An object containing the maximum number of primes | ||
* and the product of coefficients a and b. | ||
*/ | ||
function findMaxConsecutivePrimes() { | ||
/** | ||
* Checks if a number is prime. | ||
* | ||
* @param {number} n - The number to check for primality. | ||
* @returns {boolean} True if n is a prime number, false otherwise. | ||
*/ | ||
function isPrime(n) { | ||
if (n < 2) return false // 0 and 1 are not prime numbers | ||
if (n === 2) return true // 2 is a prime number | ||
if (n % 2 === 0) return false // Exclude even numbers | ||
for (let i = 3; i <= Math.sqrt(n); i += 2) { | ||
// Check odd divisors only | ||
if (n % i === 0) return false // Divisible by i, so not prime | ||
} | ||
return true // No divisors found, so it is prime | ||
} | ||
|
||
let maxPrimes = 0 // Store the maximum number of primes found | ||
let product = 0 // Store the product of coefficients a and b | ||
|
||
for (let a = -999; a < 1000; a++) { | ||
for (let b = -1000; b <= 1000; b++) { | ||
let n = 0 | ||
let consecutivePrimes = 0 | ||
while (true) { | ||
const result = n * n + a * n + b // Evaluate the quadratic expression | ||
if (result < 0 || !isPrime(result)) break // Stop if the result is negative or not prime | ||
consecutivePrimes++ | ||
n++ | ||
} | ||
if (consecutivePrimes > maxPrimes) { | ||
maxPrimes = consecutivePrimes | ||
product = a * b // Calculate product of coefficients a and b | ||
} | ||
} | ||
} | ||
|
||
return { maxPrimes, product } // Return the results | ||
} | ||
|
||
// Exporting the main function for use in other modules | ||
export { findMaxConsecutivePrimes } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { findMaxConsecutivePrimes } from '../Problem027' | ||
|
||
describe('Problem 027 - Quadratic Primes', () => { | ||
test('should return the correct product of coefficients for max consecutive primes', () => { | ||
const { maxPrimes, product } = findMaxConsecutivePrimes() | ||
expect(maxPrimes).toBe(71) | ||
expect(product).toBe(-59231) | ||
}) | ||
}) |