Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created November 30, 2011 23:28
Show Gist options
  • Select an option

  • Save jakedobkin/1411845 to your computer and use it in GitHub Desktop.

Select an option

Save jakedobkin/1411845 to your computer and use it in GitHub Desktop.
Euler 27
#!/usr/local/bin/node
// prime sieve from problem 9
n=2000000;
sumPrimes=0;
myPrimes = new Array();
for (i=2;i<=n;i++)
{
myPrimes[i]=true;
}
for (i=2;i<=n/2;i++)
{
if (myPrimes[i])
{
for (j=2*i;j<=n;j+=i)
{
myPrimes[j]=false;
}
}
}
// general plan of attack:
// first take n to like 200
// and a and b from -1000 to +1000
// break if you encounter a non-prime
// add up primes in each combo, save the highest value
biggest_total= 0;
biggest_a = 0;
biggest_b = 0;
for (a=-1000; a<=1000; a++)
{
for (b=-1000; b<=1000; b++)
{
total = 0;
for (n=0;n<=200;n++)
{
test = (n*n)+(a*n)+b;
if(myPrimes[test]!=true)
{
// console.log(a + " and " + b + "" + " generated " + total + " primes");
break;
}
else if (myPrimes[test]==true)
{
total += 1;
// console.log(test + " is the " + total + " prime");
}
}
if (total > biggest_total)
{
biggest_total= total;
biggest_a = a;
biggest_b = b;
}
}
}
console.log ("coefficients " + biggest_a + " and " + biggest_b + " have " + biggest_total + " primes in chain");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment