Skip to content

Commit

Permalink
Solve p27 in javascript, rust
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 12, 2024
1 parent e48139a commit 82b9008
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
Expand All @@ -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| |
+------------+--------------------------+--------+-------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
18 changes: 18 additions & 0 deletions docs/javascript/p0027.rst
Original file line number Diff line number Diff line change
@@ -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:
6 changes: 6 additions & 0 deletions docs/javascript/primes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
18 changes: 18 additions & 0 deletions docs/rust/p0027.rst
Original file line number Diff line number Diff line change
@@ -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:
1 change: 1 addition & 0 deletions javascript/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
1 change: 1 addition & 0 deletions javascript/euler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
40 changes: 40 additions & 0 deletions javascript/src/lib/primes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
50 changes: 50 additions & 0 deletions javascript/src/p0027.js
Original file line number Diff line number Diff line change
@@ -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');
1 change: 1 addition & 0 deletions rust/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
Expand Down
4 changes: 4 additions & 0 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod p~N;
});
mod p0022;
mod p0024;
mod p0027;
mod p0034;
mod p0069;
mod p0076;
Expand Down Expand Up @@ -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),
Expand All @@ -75,6 +77,7 @@ fn main() {
let mut answers: Vec<usize> = (1..=20).collect();
answers.push(22);
answers.push(24);
answers.push(27);
answers.push(34);
answers.push(69);
answers.push(76);
Expand Down Expand Up @@ -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)]
Expand Down
53 changes: 53 additions & 0 deletions rust/src/p0027.rs
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit 82b9008

Please sign in to comment.