Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (69 sloc) 1.24 KB
<!DOCTYPE html>
<head>
<title>GenPrime JavaScript</title>
</head>
<body>
<div id="console">
</div>
<script type="text/javascript">
function isprime(x)
{
var y;
var lim;
if (x < 2)
return false;
if (x == 2)
return true;
if (x % 2 == 0)
return false;
if (x < 9)
return true;
if ((x + 1) % 6 != 0)
if ((x - 1) % 6 != 0)
return false;
lim = Math.sqrt(x) + 1.0;
for (y = 3; y < lim; y += 2)
{
if (x % y == 0)
return false;
}
return true;
}
function genprime(max)
{
var count = 0;
var current = 1;
while (count < max)
{
if (isprime(current))
count++;
current++;
}
return current - 1;
}
function record(primes, time, last) {
document.getElementById("console").innerHTML += "Found " + primes + " primes in " + time / 1000.0 + " seconds (last was " + last + ")<br />";
}
//var start = 25000;
//var stop = 250000;
var start = 10000;
var stop = 100000;
var x;
document.getElementById("console").innerHTML += "genprime (JavaScript)<br />";
function nextTest()
{
if (x > stop)
return;
var startDate = new Date();
var last = genprime(x);
var endDate = new Date();
var duration = endDate - startDate;
record(x, duration, last);
x += start;
setTimeout(nextTest, 1, last);
}
x = start;
nextTest()
</script>
</body>
</html>