-
Notifications
You must be signed in to change notification settings - Fork 1
/
primes.js
35 lines (30 loc) · 834 Bytes
/
primes.js
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
function ones() { return stream_cons(1, ones); }
function integers(from) {
return stream_cons(from, function() {
return stream_add(ones(), integers(from));
});
}
function filter_primes(s) {
var num = stream_car(s);
return stream_cons(num,
function() {
return filter_primes(
stream_filter(s, function(v) { return v % num; })
);
});
}
var primes = filter_primes(integers(2));
var result = document.getElementById("result");
function cell(content) {
var cell = document.createElement("td");
cell.appendChild(document.createTextNode(content));
return cell;
}
for (var i = 1; i <= 20; ++i) {
var row = document.createElement("tr");
row.appendChild(cell("π(" + i + ")"));
row.appendChild(cell("=="));
row.appendChild(cell("" + stream_car(primes)));
result.appendChild(row);
primes = stream_cdr(primes);
}