Anonymous user
Miller–Rabin primality test: Difference between revisions
Added a much more readable, simpler and faster algorithm.
m ({{out}} / -Category:Scala Implementations) |
(Added a much more readable, simpler and faster algorithm.) |
||
Line 1,451:
=={{header|JavaScript}}==
For the return values of this function, <code>true</code> means "probably prime" and <code>false</code> means "definitely composite."
<lang JavaScript>function
if (n % 2 === 0 || n < 2)
}▼
if(n<25) return true;▼
▲ if(t) return false;
return true;▼
// Write (n - 1) as 2^s * d
var s = 0, d = n - 1;
while (d % 2 === 0) {
d /= 2;
++s;
▲ }
WitnessLoop: do {
// A base between 2 and n - 2
var x = Math.pow(2 + Math.floor(Math.random() * (n - 3)), d) % n;
if (x === 1 || x === n - 1)
continue;
for (var i = s - 1; i--;) {
x = x * x % n;
if (x === 1)
return false;
if (x === n - 1)
continue WitnessLoop;
}
} while (--k);
}</lang>
=={{header|Julia}}==
|