-
Notifications
You must be signed in to change notification settings - Fork 3
/
primos.html
58 lines (51 loc) · 1.38 KB
/
primos.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="Jorge">
<!-- Date: 2010-10-15 -->
<script>
function buscaPrimos (end) {
var primos= [2];
var t= +new Date();
function print (start, post) {
document.body.appendChild(
document.createElement(
'span')).innerText= primos.slice(start).join(', ')+ post;
}
(function loop (n) {
var refresh= 1e5;
var start= primos.length;
do {
var i= 0;
var esPrimo= true;
var divisor= primos[i];
var sqrt= Math.sqrt(n);
do {
if ( !(n % divisor) ) {
esPrimo= false;
break;
}
divisor= primos[++i];
} while (divisor <= sqrt);
refresh--;
if (esPrimo) primos.push(n);
if (refresh < 0) {
print(start, ", ");
return setTimeout(function () { loop(++n) }, 0);
}
++n;
} while (primos.length < end);
t= +new Date()- t;
print(start, " *** "+ end+ " : "+ t+ "ms");
})(2);
}
setTimeout(function(){buscaPrimos(1e6)}, 0);
</script>
</head>
<body>
2,</body>
</html>