1

I'm working on implementing the Sieve of Sundaram.

The first step is to get a list of Integer's such that:

i, j are Natural Numbers, 1 <= i <= j

i + j + 2*i*j <= n

Here's my function. It's supposed to generate a list of tuples of all (i, j)'s that match the above restrictions.

numsToRemove :: Integer -> [(Integer, Integer)]
numsToRemove n = [ (i, j) | i <- [1..n], j <- [1..n], i <= j, i >= 1, j >= 1, 
                            i * j + 2*i*j <= n]

But I'm getting non-primes in my answer. Excluding my other work, I think that I'm making a mistake in generating this list of Integers.

Please let me know what I'm doing wrong.

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

2

Your function checks for i * j + 2*i*j <= n but your definition asks for i + j + 2*i*j <= n. There is a * that should have been a +.

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • thanks for catching my mistake! however, when running `numsToRemove 100`, and then doing `(toList . foldr (\(x,y) acc -> insert x $ insert y acc) empty)`, I get `[1..33]` as the result... – Kevin Meredith Sep 26 '14 at 01:11
  • Why do you think thats wrong? Its what I would expect from the restriction you gave in your question. – hugomg Sep 26 '14 at 01:29
  • well, once I get `[1..33]` as the result, then it's certainly not right if I exclude all of those numbers, no? – Kevin Meredith Sep 26 '14 at 01:30
  • 2
    @KevinMeredith, The sieve removes the *results* of the calculation `i + j + 2ij`, not the `i`s and `j`s that generated them. – luqui Sep 26 '14 at 02:06