diff --git a/README.rst b/README.rst index 24286157..cb0c52a4 100644 --- a/README.rst +++ b/README.rst @@ -78,7 +78,7 @@ Olivia's Project Euler Solutions | | | | |CodeQL| |br| | | | | | |Java-lint| | +------------+--------------------------+--------+-------------------+ -| JavaScript | Node 12+ |br| | 20 | |JavaScript| |br| | +| JavaScript | Node 12+ |br| | 21 | |JavaScript| |br| | | | Bun 1.0+ |br| | | |Js-Cov| |br| | | | Firefox [2]_ |br| | | |CodeQL| |br| | | | Chrome [2]_ | | |ESLint| | @@ -88,7 +88,7 @@ Olivia's Project Euler Solutions | | GraalPy 23.1+ |br| | | |CodeQL| |br| | | | Pyodide 0.26.2+ [3]_ | | |PythonLint| | +------------+--------------------------+--------+-------------------+ -| Rust | 1.69+ | 27 | |Rust| |br| | +| Rust | 1.69+ | 28 | |Rust| |br| | | | | | |Rs-Cov| |br| | | | | | |RustClippy| | +------------+--------------------------+--------+-------------------+ diff --git a/docs/index.rst b/docs/index.rst index f309fd64..5c80818f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -119,7 +119,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`25` | | | | | |:py-d:`0025`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`27` | | | | | |:py-d:`0027`| | +|:prob:`27` | | | | |:js-d:`0027`|:py-d:`0027`|:rs-d:`0027`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`28` | | | | | |:py-d:`0028`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/docs/javascript/p0027.rst b/docs/javascript/p0027.rst new file mode 100644 index 00000000..2115e352 --- /dev/null +++ b/docs/javascript/p0027.rst @@ -0,0 +1,18 @@ +JavaScript Implementation of Problem 27 +======================================= + +View source code :source:`javascript/src/p0027.js` + +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + +.. js:autofunction:: p0027 + +.. literalinclude:: ../../javascript/src/p0027.js + :language: javascript + :linenos: diff --git a/docs/javascript/primes.rst b/docs/javascript/primes.rst index c504a7cf..5c761ccc 100644 --- a/docs/javascript/primes.rst +++ b/docs/javascript/primes.rst @@ -9,6 +9,12 @@ View source code :source:`javascript/src/lib/primes.js` .. js:autofunction:: primes.primeFactors +.. js:autofunction:: primes.primeAndNegatives + +.. js:autofunction:: primes.isComposite + +.. js:autofunction:: primes.isPrime + .. literalinclude:: ../../javascript/src/lib/primes.js :language: javascript :linenos: diff --git a/docs/rust/p0027.rst b/docs/rust/p0027.rst new file mode 100644 index 00000000..28786d74 --- /dev/null +++ b/docs/rust/p0027.rst @@ -0,0 +1,18 @@ +Rust Implementation of Problem 27 +================================= + +View source code :source:`rust/src/p0027.rs` + +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + +.. rust:fn:: p0027::p0027() -> utils::Answer + +.. literalinclude:: ../../rust/src/p0027.rs + :language: rust + :linenos: diff --git a/javascript/README.rst b/javascript/README.rst index e31eee4f..e60f5ed8 100644 --- a/javascript/README.rst +++ b/javascript/README.rst @@ -116,6 +116,7 @@ Problems Solved - ☒ `17 <./src/p0017.js>`__ - ☒ `20 <./src/p0020.js>`__ - ☒ `22 <./src/p0022.js>`__ +- ☒ `27 <./src/p0027.js>`__ - ☒ `34 <./src/p0034.js>`__ - ☒ `76 <./src/p0076.js>`__ - ☒ `836 <./src/p0836.js>`__ diff --git a/javascript/euler.test.js b/javascript/euler.test.js index d1a12bde..7e6423ce 100644 --- a/javascript/euler.test.js +++ b/javascript/euler.test.js @@ -36,6 +36,7 @@ const answers = { 17: [require('./src/p0017.js'), false], 20: [require('./src/p0020.js'), false], 22: [require('./src/p0022.js'), false], + 27: [require('./src/p0027.js'), false], 34: [require('./src/p0034.js'), false], 76: [require('./src/p0076.js'), true], 836: [require('./src/p0836.js'), false], diff --git a/javascript/src/lib/primes.js b/javascript/src/lib/primes.js index 9129047a..0f3ee777 100644 --- a/javascript/src/lib/primes.js +++ b/javascript/src/lib/primes.js @@ -128,3 +128,43 @@ function* primeFactors(num) { } } exports.primeFactors = primeFactors; + +/** + * Iterates over the prime numbers (and their negatives) up to an (optional) limit, with caching. + * + * This iterator leverages the :js:func:`primes.primes` iterator. + * @param {number | null} stop + * @yield {number} + */ +function* primesAndNegatives(stop = null) { + for (p of primes(stop)) { + yield p; + yield -p; + } +} +exports.primesAndNegatives = primesAndNegatives; + +/** + * Tests if a number is composite + * @param {number} n + * @return {number} 0 if prime, or the first prime factor if not + */ +function isComposite(n) { + const factors = primeFactors(n); + const first = factors.next().value; + if (factors.next().done) { + return 0; + } + return first; +} +exports.isComposite = isComposite; + +/** + * Tests if a number is prime + * @param {number} n + * @return {boolean} + */ +function isPrime(n) { + return !isComposite(n); +} +exports.isPrime = isPrime; diff --git a/javascript/src/p0027.js b/javascript/src/p0027.js new file mode 100644 index 00000000..94e79e4f --- /dev/null +++ b/javascript/src/p0027.js @@ -0,0 +1,50 @@ +/** + * Project Euler Problem 27 + * + * Another good problem for code golf + * + * Problem: Euler discovered the remarkable quadratic formula: + * + * n**2+n+41 + * + * It turns out that the formula will produce 40 primes for the consecutive + * integer values 0≤n≤39. However, when ``n=40``, ``40**2+40+41=40(40+1)+41`` is divisible + * by 41, and certainly when ``n=41``, ``41**2+41+41`` is clearly divisible by 41. + * + * The incredible formula ``n**2−79n+1601`` was discovered, which produces 80 primes + * for the consecutive values 0≤n≤79. The product of the coefficients, −79 and + * 1601, is −126479. + * + * Considering quadratics of the form: + * + * n**2+an+b + * + * , where ``|a|<1000`` and ``|b|≤1000`` + * + * where ``|n|`` is the modulus/absolute value of n, e.g. ``|11|=11`` and ``|−4|=4`` + * + * 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, + * starting with n=0. + * + * @return {number} + */ +exports.p0027 = function() { + let streak = 0; + let answer = 0; + for (let a = -999; a < 1000; a++) { + for (b of primes.primesAndNegatives(1001)) { + let i = 0; + while (primes.isPrime((i + a) * i + b)) { + i++; + } + if (i > streak) { + streak = i; + answer = a * b; + } + } + } + return answer; +}; + +const primes = require('./lib/primes.js'); diff --git a/rust/README.rst b/rust/README.rst index 6246c175..05677959 100644 --- a/rust/README.rst +++ b/rust/README.rst @@ -79,6 +79,7 @@ Problems Solved - ☒ `20 <./src/p0020.rs>`__ - ☒ `22 <./src/p0022.rs>`__ - ☒ `24 <./src/p0024.rs>`__ +- ☒ `27 <./src/p0027.rs>`__ - ☒ `34 <./src/p0034.rs>`__ - ☒ `69 <./src/p0069.rs>`__ - ☒ `76 <./src/p0076.rs>`__ diff --git a/rust/src/main.rs b/rust/src/main.rs index f16ff706..c7915103 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -20,6 +20,7 @@ mod p~N; }); mod p0022; mod p0024; +mod p0027; mod p0034; mod p0069; mod p0076; @@ -55,6 +56,7 @@ fn get_problem<'b>(n: usize) -> ProblemRef<'b> { 20 => ( &20, p0020::p0020, false), 22 => ( &22, p0022::p0022, false), 24 => ( &24, p0024::p0024, false), + 27 => ( &27, p0027::p0027, false), 34 => ( &34, p0034::p0034, false), 69 => ( &69, p0069::p0069, false), 76 => ( &76, p0076::p0076, false), @@ -75,6 +77,7 @@ fn main() { let mut answers: Vec = (1..=20).collect(); answers.push(22); answers.push(24); + answers.push(27); answers.push(34); answers.push(69); answers.push(76); @@ -102,6 +105,7 @@ seq!(N in 01..=20 { )* #[case::problem_22(22)] #[case::problem_24(24)] +#[case::problem_27(27)] #[case::problem_34(34)] #[case::problem_69(69)] #[case::problem_76(76)] diff --git a/rust/src/p0027.rs b/rust/src/p0027.rs new file mode 100644 index 00000000..8f80ff5a --- /dev/null +++ b/rust/src/p0027.rs @@ -0,0 +1,53 @@ +/* +Project Euler Problem 27 + +Another good problem for code golf + +Problem:Euler discovered the remarkable quadratic formula: + +n**2+n+41 + +It turns out that the formula will produce 40 primes for the consecutive +integer values 0≤n≤39. However, when ``n=40``, ``40**2+40+41=40(40+1)+41`` is divisible +by 41, and certainly when ``n=41``, ``41**2+41+41`` is clearly divisible by 41. + +The incredible formula ``n**2−79n+1601`` was discovered, which produces 80 primes +for the consecutive values 0≤n≤79. The product of the coefficients, −79 and +1601, is −126479. + +Considering quadratics of the form: + + n**2+an+b + +, where ``|a|<1000`` and ``|b|≤1000`` + +where ``|n|`` is the modulus/absolute value of n, e.g. ``|11|=11`` and ``|−4|=4`` + +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, +starting with n=0. +*/ +use crate::primes::{is_prime,primes_until}; +use crate::include::utils::Answer; + +pub fn p0027() -> Answer { + let mut streak: u64 = 0; + let mut answer: i64 = 0; + let mut a: i64; + for a in (-999)..1000 { + for b in primes_until(1001) { + for factor in !vec[1, -1] { + let bp = b * factor; + let mut i = 0; + while is_prime((i + a) * i + bp) { + i += 1; + } + if i > streak { + streak = i; + answer = a * bp; + } + } + } + } + return Answer::Int(answer.into()); +}