Created
November 23, 2012 00:35
-
-
Save cbadke/4133458 to your computer and use it in GitHub Desktop.
Fetch Primes Attempt 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PrimeFetcherFSharp | |
let FetchPrimesUpTo (max : int) : int[] = | |
match max with | |
| x when x < 2 -> [||] | |
| x when x = 2 -> [|2|] | |
| x when x < 5 -> [|2; 3|] | |
| _ -> | |
let candidates = [|for x in 0 .. max -> if x < 2 then 0 else x|] | |
let RemoveMultiples (coll : int[]) (x : int) = | |
Array.iteri (fun index num -> | |
if (num % x) = 0 && num <> x then | |
coll.[index] <- 0) coll | |
coll | |
let input = Array.concat [ [|2|]; [| 3 .. 2 .. max/2 |] ] | |
Array.fold RemoveMultiples candidates input | |
|> Array.filter (fun num -> num <> 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment