Scientific Calculator
moshangcheng/sievePrime
By moshangcheng
The linear Sieve of Eratosthenes
Tagged:
sievePrime = function(n) {
var prime = [];
var notP = range(1, n+1).*0;
var i = 2;
while(i <= n) -> (
(not notP[i]) ? push(prime, i) : "";
var j = 0;
var flag = 1;
var condition := flag and j < len prime and prime[j]*i <= n;
while(condition ) -> (
notP[i * prime[j] ] = 1;
flag = (i mod prime[j]) ? 1 : 0;
j += 1;
);
i += 1;
);
prime;
};
1 Comment
I like your "range(1, n+1).*0" expression, but it makes me think I need to add a "zeros(n)" function or something similar.
Sign in to leave a comment