/** This code was based on Google I/O 2012:Breaking the JavaScript Speed Limit with V8(http://www.youtube.com/watch?v=UJPdhx5zTaw) **/functionPrimes(){this.prime_count=0;this.primes=newArray(50000);};Primes.prototype.getPrimeCount=function(){returnthis.prime_count;}Primes.prototype.getPrime=function(i){returnthis.primes[i];}Primes.prototype.addPrime=function(i){this.primes[this.prime_count++]=i;}Primes.prototype.isPrimeDivisible=function(candidate){for(vari=1;i<this.prime_count;++i){if((candidate%this.primes[i])==0)returntrue;}returnfalse;}functionmain(){p=newPrimes();varc=1;varst=Date.now();while(p.getPrimeCount()<50000){if(!p.isPrimeDivisible(c)){p.addPrime(c);}c++;}console.log((Date.now()-st));console.log(p.getPrime(p.getPrimeCount()-1));}main();
/** This code was based on Google I/O 2012: Breaking the JavaScript Speed Limit with V8 (http://www.youtube.com/watch?v=UJPdhx5zTaw) **/#include <stdio.h>;#include <sys/time.h>;#include <sys/types.h>;staticint64_tmicroSecondOfNow(){structtimevalt;gettimeofday(&t,NULL);return((int64_t)t.tv_sec)*(1000*1000)+t.tv_usec;}classPrimes{public:intgetPrimeCount()const{returnprime_count;}intgetPrime(inti)const{returnprimes[i];}voidaddPrime(inti){primes[prime_count++]=i;}boolisPrimeDivisible(intcandidate){for(inti=1;i<prime_count;++i){if((candidate%primes[i])==0)returntrue;}returnfalse;}private:volatileintprime_count;volatileintprimes[50000];};intmain(){Primesp;intc=1;int64_tst=microSecondOfNow();while(p.getPrimeCount()<50000){if(!p.isPrimeDivisible(c)){p.addPrime(c);}c++;}printf("%lld\n",(microSecondOfNow()-st)/1000);printf("%d\n",p.getPrime(p.getPrimeCount()-1));}