Skip to content

Instantly share code, notes, and snippets.

@thegreatshasha
Created August 14, 2014 10:43
Show Gist options
  • Save thegreatshasha/0df84a9f17dc66dfbb2c to your computer and use it in GitHub Desktop.
Save thegreatshasha/0df84a9f17dc66dfbb2c to your computer and use it in GitHub Desktop.
module.exports = function(){
// The theoretical bound on number of integers necessary to generate n primes
this.upperBound = function(num) {
return parseInt(num*(Math.log(num)+Math.log(Math.log(num))));
}
this.composites = {}
this.currentPrime = 1
this.primes = [2]
this.calculate = function(numOfPrimes) {
var max = this.upperBound(numOfPrimes)
var count = 1;
while(count<numOfPrimes) {
this.currentPrime += 2;
if(!this.composites[this.currentPrime]){
count += 1;
this.primes.push(this.currentPrime)
for(var i = this.currentPrime; i<= max; i += 2*this.currentPrime) {
this.composites[i] = true
}
}
}
return this.primes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment