Created
November 30, 2011 23:28
-
-
Save jakedobkin/1411845 to your computer and use it in GitHub Desktop.
Euler 27
This file contains hidden or 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
| #!/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