Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -0,0 +1,27 @@
|
|||
function eratosthenes(limit) {
|
||||
var primes = [];
|
||||
if (limit >= 2) {
|
||||
var sqrtlmt = Math.sqrt(limit) - 2;
|
||||
var nums = new Array(); // start with an empty Array...
|
||||
for (var i = 2; i <= limit; i++) // and
|
||||
nums.push(i); // only initialize the Array once...
|
||||
for (var i = 0; i <= sqrtlmt; i++) {
|
||||
var p = nums[i]
|
||||
if (p)
|
||||
for (var j = p * p - 2; j < nums.length; j += p)
|
||||
nums[j] = 0;
|
||||
}
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
var p = nums[i];
|
||||
if (p)
|
||||
primes.push(p);
|
||||
}
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
var primes = eratosthenes(100);
|
||||
|
||||
if (typeof print == "undefined")
|
||||
print = (typeof WScript != "undefined") ? WScript.Echo : alert;
|
||||
print(primes);
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
function eratosthenes(limit) {
|
||||
var prms = [];
|
||||
if (limit >= 2) prms = [2];
|
||||
if (limit >= 3) {
|
||||
var sqrtlmt = (Math.sqrt(limit) - 3) >> 1;
|
||||
var lmt = (limit - 3) >> 1;
|
||||
var bfsz = (lmt >> 5) + 1
|
||||
var buf = [];
|
||||
for (var i = 0; i < bfsz; i++)
|
||||
buf.push(0);
|
||||
for (var i = 0; i <= sqrtlmt; i++)
|
||||
if ((buf[i >> 5] & (1 << (i & 31))) == 0) {
|
||||
var p = i + i + 3;
|
||||
for (var j = (p * p - 3) >> 1; j <= lmt; j += p)
|
||||
buf[j >> 5] |= 1 << (j & 31);
|
||||
}
|
||||
for (var i = 0; i <= lmt; i++)
|
||||
if ((buf[i >> 5] & (1 << (i & 31))) == 0)
|
||||
prms.push(i + i + 3);
|
||||
}
|
||||
return prms;
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
var SoEIncClass = (function () {
|
||||
function SoEIncClass() {
|
||||
this.n = 0;
|
||||
}
|
||||
SoEIncClass.prototype.next = function () {
|
||||
this.n += 2;
|
||||
if (this.n < 7) { // initialization of sequence to avoid runaway:
|
||||
if (this.n < 3) { // only even of two:
|
||||
this.n = 1; // odds from here...
|
||||
return 2;
|
||||
}
|
||||
if (this.n < 5)
|
||||
return 3;
|
||||
this.dict = {}; // n must be 5...
|
||||
this.bps = new SoEIncClass(); // new source of base primes
|
||||
this.bps.next(); // advance past the even prime of two...
|
||||
this.p = this.bps.next(); // first odd prime (3 in this case)
|
||||
this.q = this.p * this.p; // set guard
|
||||
return 5;
|
||||
} else { // past initialization:
|
||||
var s = this.dict[this.n]; // may or may not be defined...
|
||||
if (!s) { // not defined:
|
||||
if (this.n < this.q) // haven't reached the guard:
|
||||
return this.n; // found a prime
|
||||
else { // n === q => not prime but at guard, so:
|
||||
var p2 = this.p << 1; // the span odds-only is twice prime
|
||||
this.dict[this.n + p2] = p2; // add next composite of prime to dict
|
||||
this.p = this.bps.next();
|
||||
this.q = this.p * this.p; // get next base prime guard
|
||||
return this.next(); // not prime so advance...
|
||||
}
|
||||
} else { // is a found composite of previous base prime => not prime
|
||||
delete this.dict[this.n]; // advance to next composite of this prime:
|
||||
var nxt = this.n + s;
|
||||
while (this.dict[nxt]) nxt += s; // find unique empty slot in dict
|
||||
this.dict[nxt] = s; // to put the next composite for this base prime
|
||||
return this.next(); // not prime so advance...
|
||||
}
|
||||
}
|
||||
};
|
||||
return SoEIncClass;
|
||||
})();
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
var gen = new SoEIncClass();
|
||||
for (var i = 1; i < 1000000; i++, gen.next());
|
||||
var prime = gen.next();
|
||||
|
||||
if (typeof print == "undefined")
|
||||
print = (typeof WScript != "undefined") ? WScript.Echo : alert;
|
||||
print(prime);
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
var SoEPgClass = (function () {
|
||||
function SoEPgClass() {
|
||||
this.bi = -1; // constructor resets the enumeration to start...
|
||||
}
|
||||
SoEPgClass.prototype.next = function () {
|
||||
if (this.bi < 1) {
|
||||
if (this.bi < 0) {
|
||||
this.bi++;
|
||||
this.lowi = 0; // other initialization done here...
|
||||
this.bpa = [];
|
||||
return 2;
|
||||
} else { // bi must be zero:
|
||||
var nxt = 3 + (this.lowi << 1) + 262144;
|
||||
this.buf = new Array();
|
||||
for (var i = 0; i < 4096; i++) // faster initialization:
|
||||
this.buf.push(0);
|
||||
if (this.lowi <= 0) { // special culling for first page as no base primes yet:
|
||||
for (var i = 0, p = 3, sqr = 9; sqr < nxt; i++, p += 2, sqr = p * p)
|
||||
if ((this.buf[i >> 5] & (1 << (i & 31))) === 0)
|
||||
for (var j = (sqr - 3) >> 1; j < 131072; j += p)
|
||||
this.buf[j >> 5] |= 1 << (j & 31);
|
||||
} else { // after the first page:
|
||||
if (!this.bpa.length) { // if this is the first page after the zero one:
|
||||
this.bps = new SoEPgClass(); // initialize separate base primes stream:
|
||||
this.bps.next(); // advance past the only even prime of two
|
||||
this.bpa.push(this.bps.next()); // get the next prime (3 in this case)
|
||||
}
|
||||
// get enough base primes for the page range...
|
||||
for (var p = this.bpa[this.bpa.length - 1], sqr = p * p; sqr < nxt;
|
||||
p = this.bps.next(), this.bpa.push(p), sqr = p * p) ;
|
||||
for (var i = 0; i < this.bpa.length; i++) {
|
||||
var p = this.bpa[i];
|
||||
var s = (p * p - 3) >> 1;
|
||||
if (s >= this.lowi) // adjust start index based on page lower limit...
|
||||
s -= this.lowi;
|
||||
else {
|
||||
var r = (this.lowi - s) % p;
|
||||
s = (r != 0) ? p - r : 0;
|
||||
}
|
||||
for (var j = s; j < 131072; j += p)
|
||||
this.buf[j >> 5] |= 1 << (j & 31);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while (this.bi < 131072 && this.buf[this.bi >> 5] & (1 << (this.bi & 31)))
|
||||
this.bi++; // find next marker still with prime status
|
||||
if (this.bi < 131072) // within buffer: output computed prime
|
||||
return 3 + ((this.lowi + this.bi++) << 1);
|
||||
else { // beyond buffer range: advance buffer
|
||||
this.bi = 0;
|
||||
this.lowi += 131072;
|
||||
return this.next(); // and recursively loop
|
||||
}
|
||||
};
|
||||
return SoEPgClass;
|
||||
})();
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
function erasthenes(limit) {
|
||||
var primes = [];
|
||||
if (limit >= 2) {
|
||||
var nums = new Array(limit-1);
|
||||
for (var i = 2; i <= limit; i++)
|
||||
nums[i-2] = i;
|
||||
|
||||
var last_prime;
|
||||
var idx = 0;
|
||||
while ((last_prime = nums[idx]) <= Math.sqrt(limit)) {
|
||||
if (last_prime != null)
|
||||
for (var i = idx + last_prime; i < limit - 1; i += last_prime)
|
||||
nums[i] = null;
|
||||
idx++;
|
||||
}
|
||||
for (var i = 0; i < nums.length; i++)
|
||||
if (nums[i] != null)
|
||||
primes.push(nums[i]);
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
var primes = erasthenes(100);
|
||||
|
||||
if (typeof print == "undefined")
|
||||
print = (typeof WScript != "undefined") ? WScript.Echo : alert;
|
||||
print(primes);
|
||||
Loading…
Add table
Add a link
Reference in a new issue