-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e48139a
commit 9159943
Showing
12 changed files
with
196 additions
and
3 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
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
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,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: |
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
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,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: |
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
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
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
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,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'); |
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
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
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,54 @@ | ||
/* | ||
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; | ||
let factors: Vec<i64> = vec![1, -1]; | ||
for a in (-999)..1000 { | ||
for b in primes_until(1001) { | ||
for factor in factors.iter() { | ||
let bp = b * factor; | ||
let mut i: u64 = 0; | ||
while is_prime((i + a) * i + bp) { | ||
i += 1; | ||
} | ||
if i > streak { | ||
streak = i; | ||
answer = a * bp; | ||
} | ||
} | ||
} | ||
} | ||
return Answer::Int(answer.into()); | ||
} |