Skip to main content
Commonmark migration
Source Link

#Node.js (JavaScript/ES6), 83.549s (11 Nov 2016)

Node.js (JavaScript/ES6), 83.549s (11 Nov 2016)

var n=process.argv[2]*1,r=new Uint8Array(n),p=0,i=1,j while(++i<=n){ if(r[i]===0){ for(j=i*i;j<=n;j+=i){r[j]=1} p+=1 } } console.log(p) 

Finally got around to remaking this, and it's both smaller/simpler and MUCH faster than before. Rather than a slower brute force method, it utilizes the Sieve of Eratosthenes alongside more efficient data structures, so that it is now able to actually finish in a respectable time (as far as I can find on the internet, it's the fastest JS prime count function out there).

Some demo times (i7-3770k):

10^4 (10,000) => 0.001 seconds 10^5 (100,000) => 0.003 seconds 10^6 (1,000,000) => 0.009 seconds 10^7 (10,000,000) => 0.074 seconds 10^8 (100,000,000) => 1.193 seconds 10^9 (1,000,000,000) => 14.415 seconds 

#Node.js (JavaScript/ES6), 83.549s (11 Nov 2016)

var n=process.argv[2]*1,r=new Uint8Array(n),p=0,i=1,j while(++i<=n){ if(r[i]===0){ for(j=i*i;j<=n;j+=i){r[j]=1} p+=1 } } console.log(p) 

Finally got around to remaking this, and it's both smaller/simpler and MUCH faster than before. Rather than a slower brute force method, it utilizes the Sieve of Eratosthenes alongside more efficient data structures, so that it is now able to actually finish in a respectable time (as far as I can find on the internet, it's the fastest JS prime count function out there).

Some demo times (i7-3770k):

10^4 (10,000) => 0.001 seconds 10^5 (100,000) => 0.003 seconds 10^6 (1,000,000) => 0.009 seconds 10^7 (10,000,000) => 0.074 seconds 10^8 (100,000,000) => 1.193 seconds 10^9 (1,000,000,000) => 14.415 seconds 

Node.js (JavaScript/ES6), 83.549s (11 Nov 2016)

var n=process.argv[2]*1,r=new Uint8Array(n),p=0,i=1,j while(++i<=n){ if(r[i]===0){ for(j=i*i;j<=n;j+=i){r[j]=1} p+=1 } } console.log(p) 

Finally got around to remaking this, and it's both smaller/simpler and MUCH faster than before. Rather than a slower brute force method, it utilizes the Sieve of Eratosthenes alongside more efficient data structures, so that it is now able to actually finish in a respectable time (as far as I can find on the internet, it's the fastest JS prime count function out there).

Some demo times (i7-3770k):

10^4 (10,000) => 0.001 seconds 10^5 (100,000) => 0.003 seconds 10^6 (1,000,000) => 0.009 seconds 10^7 (10,000,000) => 0.074 seconds 10^8 (100,000,000) => 1.193 seconds 10^9 (1,000,000,000) => 14.415 seconds 
added 83 characters in body
Source Link
Mwr247
  • 3.6k
  • 19
  • 39

#Node.js (JavaScript/ES6), 83.549s (11 Nov 2016)

var n=process.argv[2]*1,r=new Uint8Array(n),p=0,i=1,j while(++i<=n){ if(r[i]===0){ for(j=i*i;j<=n;j+=i){r[j]=1} p+=1 } } console.log(p) 

Finally got around to remaking this, and it's both smaller/simpler and MUCH faster than before. Rather than a slower brute force method, it utilizes the Sieve of Eratosthenes alongside more efficient data structures, so that it is now able to actually finish in a respectable time (as far as I can find on the internet, it's the fastest JS prime count function out there).

Some demo times (i7-4600M, laptop3770k):

10^4 (10,000) => 0.003001 seconds 10^5 (100,000) => 0.005003 seconds 10^6 (1,000,000) => 0.009 seconds 10^7 (10,000,000) => 0.093074 seconds 10^8 (100,000,000) => 1.193 seconds 10^9 (1,000,000,000) => 14.415 seconds 

#Node.js (JavaScript/ES6), 83.549s (11 Nov 2016)

var n=process.argv[2]*1,r=new Uint8Array(n),p=0,i=1,j while(++i<=n){ if(r[i]===0){ for(j=i*i;j<=n;j+=i){r[j]=1} p+=1 } } console.log(p) 

Finally got around to remaking this, and it's both smaller/simpler and MUCH faster than before. Rather than a slower brute force method, it utilizes the Sieve of Eratosthenes alongside more efficient data structures, so that it is now able to actually finish in a respectable time.

Some demo times (i7-4600M, laptop):

10^4 (10,000) => 0.003 seconds 10^5 (100,000) => 0.005 seconds 10^6 (1,000,000) => 0.009 seconds 10^7 (10,000,000) => 0.093 seconds 10^8 (100,000,000) => 1.193 seconds 10^9 (1,000,000,000) => 14.415 seconds 

#Node.js (JavaScript/ES6), 83.549s (11 Nov 2016)

var n=process.argv[2]*1,r=new Uint8Array(n),p=0,i=1,j while(++i<=n){ if(r[i]===0){ for(j=i*i;j<=n;j+=i){r[j]=1} p+=1 } } console.log(p) 

Finally got around to remaking this, and it's both smaller/simpler and MUCH faster than before. Rather than a slower brute force method, it utilizes the Sieve of Eratosthenes alongside more efficient data structures, so that it is now able to actually finish in a respectable time (as far as I can find on the internet, it's the fastest JS prime count function out there).

Some demo times (i7-3770k):

10^4 (10,000) => 0.001 seconds 10^5 (100,000) => 0.003 seconds 10^6 (1,000,000) => 0.009 seconds 10^7 (10,000,000) => 0.074 seconds 10^8 (100,000,000) => 1.193 seconds 10^9 (1,000,000,000) => 14.415 seconds 
Completely rewritten, much smaller and very much faster
Source Link
Mwr247
  • 3.6k
  • 19
  • 39

#JavaScript#Node.js (JavaScript/ NodeES6), 83.549s (ES611 Nov 2016)

functionvar Fn=process.argv[2]*1,r=new Uint8Array(n){ var i=0,a=3,b=1,p=[2]p=0,l=0i=1,s=2j  while(a<=n++i<=n){   if(a%(s=p[i])r[i]===0){   iffor(b<s||i===lj=i*i;j<=n;j+=i){p[++l]=ar[j]=1}   else{++i;continue}p+=1   }b=Math.sqrt(a+=2)|(i=0)  }return l+1- console.log(n<2p) } 

Builds upFinally got around to remaking this, and it's both smaller/simpler and MUCH faster than before. Rather than a prime list asslower brute force method, it goesutilizes the Sieve of Eratosthenes alongside more efficient data structures, checking if a is divisible by every prime inso that list up through prime #sqrt(a), and short circuiting along the way if not primeit is now able to actually finish in a respectable time.

Some demo times (i7-3770k4600M, laptop):

10^4 (10,000) => 0.001003 seconds 10^5 (100,000) => 0.005 seconds 10^6 (1,000,000) => 0.07009 seconds 10^7 (10,000,000) => 10.373093 seconds 10^8 (100,000,000) => 291.9193 seconds 10^9 (1,000,000,000) => 14.415 seconds 

#JavaScript / Node (ES6)

function F(n){ var i=0,a=3,b=1,p=[2],l=0,s=2  while(a<=n){   if(a%(s=p[i])){   if(b<s||i===l){p[++l]=a}   else{++i;continue}   }b=Math.sqrt(a+=2)|(i=0)  }return l+1-(n<2) } 

Builds up a prime list as it goes, checking if a is divisible by every prime in that list up through prime #sqrt(a), and short circuiting along the way if not prime.

Some demo times (i7-3770k):

10^4 (10,000) => 0.001 seconds 10^5 (100,000) => 0.005 seconds 10^6 (1,000,000) => 0.07 seconds 10^7 (10,000,000) => 1.373 seconds 10^8 (100,000,000) => 29.9 seconds 

#Node.js (JavaScript/ES6), 83.549s (11 Nov 2016)

var n=process.argv[2]*1,r=new Uint8Array(n),p=0,i=1,j while(++i<=n){ if(r[i]===0){ for(j=i*i;j<=n;j+=i){r[j]=1} p+=1 } } console.log(p) 

Finally got around to remaking this, and it's both smaller/simpler and MUCH faster than before. Rather than a slower brute force method, it utilizes the Sieve of Eratosthenes alongside more efficient data structures, so that it is now able to actually finish in a respectable time.

Some demo times (i7-4600M, laptop):

10^4 (10,000) => 0.003 seconds 10^5 (100,000) => 0.005 seconds 10^6 (1,000,000) => 0.009 seconds 10^7 (10,000,000) => 0.093 seconds 10^8 (100,000,000) => 1.193 seconds 10^9 (1,000,000,000) => 14.415 seconds 
made smaller for the heck of it, and also slightly faster
Source Link
Mwr247
  • 3.6k
  • 19
  • 39
Loading
wrong number
Source Link
Mwr247
  • 3.6k
  • 19
  • 39
Loading
runtimes
Source Link
Mwr247
  • 3.6k
  • 19
  • 39
Loading
More compact
Source Link
Mwr247
  • 3.6k
  • 19
  • 39
Loading
Source Link
Mwr247
  • 3.6k
  • 19
  • 39
Loading