Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created November 13, 2011 18:14
Show Gist options
  • Save jakedobkin/1362432 to your computer and use it in GitHub Desktop.
Save jakedobkin/1362432 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/node
//NOW WITH NODE.JS
n=2000000;
sumPrimes=0;
myPrimes = new Array();
// set all array values to true
for (i=2;i<=n;i++)
{
myPrimes[i]=true;
}
// sieve of eratosthenes
for (i=2;i<=n/2;i++)
{
if (myPrimes[i])
{
for (j=2*i;j<=n;j+=i)
{
myPrimes[j]=false;
}
}
}
// add up unsieved primes
for (i=2;i<=n;i++)
{
if(myPrimes[i])
{
sumPrimes+=i;
}
}
console.log(sumPrimes);
@jakedobkin
Copy link
Author

Yes- it is smart to use just one loop!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment