Skip to content

Instantly share code, notes, and snippets.

@sleexyz
Created February 4, 2013 04:29
Show Gist options
  • Save sleexyz/4705027 to your computer and use it in GitHub Desktop.
Save sleexyz/4705027 to your computer and use it in GitHub Desktop.
/*
* Problem 50 - Project Euler
* Sean Lee 02/03/12
*/
var isPrime = []
var primes = []
var genprimes = function(upperbound){ //Sieve of Erastothenes
for (var i = 0; i< upperbound; i++){
isPrime.push(1);
}
isPrime[0] = 0;
isPrime[1] = 0;
var i = 2;
var loopbound = Math.sqrt(upperbound);
while( i <= loopbound){
for( var k = 2*i; k < upperbound; k = k + i){
isPrime[k] = 0;
}
i++;
}
for ( var i = 2; i < upperbound; i ++){
if(isPrime[i] === 1) primes.push(i);
}
}
function find(upperbound){
var sums = []
var longest = [0,0];
var i = 0;
while (true){
for( var a in sums){
sums[a] += primes[i];
if (sums[a] > upperbound) return longest;
if (isPrime[sums[a]]){
var length = sums.length + 1 - a;
if (length > longest[1]){
longest = [sums[a], length];
}
}
}
sums.push(primes[i]);
i++;
}
}
genprimes(1000000);
console.log(find(1000000)[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment