Skip to content

Instantly share code, notes, and snippets.

@SaitoAtsushi
Created October 6, 2010 15:27
Show Gist options
  • Save SaitoAtsushi/613523 to your computer and use it in GitHub Desktop.
Save SaitoAtsushi/613523 to your computer and use it in GitHub Desktop.
function Primes() {
this.index=3;
this.nums=[2];
}
Primes.prototype.next=function() {
var i;
for(i=this.index; !(this.primep(i)); i++);
this.index=i+1;
this.nums.push(i);
return this.nums[this.nums.length-2];
};
Primes.prototype.primep=function(x) {
var limit=Math.floor(Math.sqrt(x));
var i;
for(i=0; this.nums[i]<=limit && x%this.nums[i]; i++){};
return this.nums[i]>limit;
};
function enumerate(ps, n) {
var r=[];
for(var i=0; i<n; i++) r.push(ps.next());
return r;
}
var primes = new Primes();
print(enumerate(primes, 10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment