Created
December 5, 2011 17:53
-
-
Save jakedobkin/1434559 to your computer and use it in GitHub Desktop.
Euler 35
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 | |
| // first, prep prime seive up to 1MM | |
| n=1000000; | |
| myPrimes = new Array(); | |
| for (i=2;i<=n;i++) | |
| { | |
| myPrimes[i]=true; | |
| } | |
| for (i=2;i<=n;i++) | |
| { | |
| if (myPrimes[i]) | |
| { | |
| for (j=2*i;j<=n;j+=i) | |
| { | |
| myPrimes[j]=false; | |
| } | |
| } | |
| } | |
| // then check if each prime is circular | |
| // if it is, add it to the de-duping array (circularsieve) | |
| circularsieve = []; | |
| for (i=0;i<=1000000;i++) | |
| { | |
| if (myPrimes[i]) | |
| { | |
| carousel = i.toString(); | |
| test = []; | |
| for (j=0;j<=carousel.length-1;j++) | |
| { | |
| copy = carousel; | |
| a = carousel.slice(0,1); | |
| b = carousel.slice(1); | |
| carousel = b + a; | |
| if(myPrimes[Number(carousel)]!=true) | |
| { | |
| break; | |
| } | |
| if(myPrimes[Number(carousel)]==true && j==carousel.length-1) | |
| { | |
| circularsieve[Number(copy)] = true; | |
| } | |
| } | |
| } | |
| } | |
| // count up all the circular primes in the de-duping array | |
| count = 0; | |
| for (k=0;k<=circularsieve.length;k++) | |
| { | |
| if (circularsieve[k]) | |
| { | |
| // console.log(k,"is a circular prime"); | |
| count += 1; | |
| } | |
| } | |
| console.log("total count is", count); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment