Hello all,
I have a question on the Sieve of Atkins. I have implemented the Sieve of Eratosthenes, and have confirmed that it is working without any problems. Now, I wanted to implement SoA to save on time when calculating primes.
My implementation is based on the pseudo-code found at http://en.wikipedia.org/wiki/Sieve_of_Atkin. I know I probably shouldn't trust wikipedia too much, but the code seems to make sense. However, there is a problem with generating primes in that the code below doesn't weed out all composite numbers.
If I change the multiples I am looking for (function mults to search for all multiples of a number vs. all multiples of its square, it eliminates all composites. However, from reading discussions around the web, the consensus seems to be that eliminating multiples of a prime numbers square *should* be enough. Obviously, I am missing a step. However, I'm not sure what that step is.
EDIT: 65, 85 and 91 are showing up when running soa 100I, and they shouldn't (as they are composites). I have worked it out by hand, and it makes sense for the function to let those numbers through as primes. So, my question is, am I misunderstanding the pseudocode or is the pseudocode incorrect?
// Sieve of Atkin **********DOESN'T WORK YET*********************
let soa limit =
let sqlmt = BigInt.to_float limit |> sqrt |> ceil |> Float.to_string
|> BigInt.of_string
let cand = [ for x in 1I .. sqlmt
for y in 1I .. sqlmt
let a = 4I*x*x + y*y
let b = 3I*x*x + y*y
let c = 3I*x*x - y*y
if a <= limit && (a % 12I = 1I || a % 12I = 5I) then yield a
if b <= limit && b % 12I = 7I then yield b
if x > y && c <= limit && c % 12I = 11I then yield c ]
|> Set.of_list
let mults p = p :: [ (p*p) .. (p*p) .. limit ] |> Set.of_list
let prune y = Set.min_elt y, Set.min_elt y |> mults |> Set.diff y
let rec nsoe' (p, pset) ans =
match pset with
| x when x = Set.empty || p > sqlmt -> Set.add p ans |> Set.union pset
| _ -> nsoe' (prune pset) (Set.add p ans)
nsoe' (prune cand) Set.empty
|> Set.add 2I |> Set.add 3I |> Set.to_list
Any help is greatly appreciated.
Thanks and regards,
z.