[projectEuler.net] 10

Sieve of Eratosthenes

 is a simple, ancient algorithm for finding all prime numbers up to any given limit.

Sieve of Eratosthenes: algorithm steps for primes below 121 (including optimization of starting from prime's square).

///Sieve of Eratosthenes
///http://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
///内部使用byte数组存放标志位,所以不适合非常大的素数范围。视内存限制。
let PrimesBelowSOE x =
        let mutable ret =[]
        let flagarr=Array.create (x+1) 0uy 
        let rec loop (arr:byte []) i x s=
                if i*s <= x then
                        arr.[i*s]<-1uy
                        loop arr i x (s+1)
        for i in 2..x do
                if flagarr.[i]= 0uy then 
                        ret<-i::ret
                        loop flagarr i x 2
        ret  


原文地址:https://www.cnblogs.com/jiangzhen/p/2331230.html