Sieve of Eratosthenes
is a simple, ancient algorithm for finding all prime numbers up to any given limit.
///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