Computer Science Canada

Elimination of multiples - Sieve of Eratosthenes

Author:  nba [ Mon Jun 08, 2009 5:28 pm ]
Post subject:  Elimination of multiples - Sieve of Eratosthenes

What is the problem you are having?
eliminating the multiples


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)


Turing:


var list : array 2 .. n of int
put skip
for i : 2 .. n
    put i : 5 ..
end for
put skip
put skip
for i : 2 .. n
    if i mod 2 >= 1  then
        put i : 5 ..
    end if
end for
put skip
for i : 2 .. n
    if i mod 3 >= 1 then
        put i : 5 ..
    end if
end for





when i give n value as 8, it outputs

Quote:

2 , 3 , 4 , 5 , 6 , 7 , 8
% Eliminates all the multiples of 2 in the row below
3 , 5 , 7
% Eliminates all the multiples of 3 in the row below
2 , 4 , 5 , 7 , 8


as u cud see, the program eliminated the multiples of 2 in second row using the first row list. then the program eliminated the multiples of 3 in third row using the first row list. I want the program to eliminate the multiples of 3 using the second row list. Juz like tht i wanna eliminate multiples of 5 using the previous list.

Please help me ASAP

and cud u please tell me how to repeat multiples by itself. i dont wanna use different for loops for mod 2, mod 3, mod 5, mod 7..
it says "The process continues until it is no longer possible to eliminate any multiples of values that are left in the table. The remaining values must be prime."

this is project is due in 2 days so please help as soon as possible
thanks a lot

Author:  Tony [ Mon Jun 08, 2009 5:36 pm ]
Post subject:  RE:Elimination of multiples - Sieve of Eratosthenes

it's not using the "first row list". In fact, it's not using any list. You create your "list" variable, and never refer to it again.

Author:  nba [ Mon Jun 08, 2009 6:22 pm ]
Post subject:  RE:Elimination of multiples - Sieve of Eratosthenes

oh sry then somewhere in the program
list (i) := i

where does this line go?

Author:  Tony [ Mon Jun 08, 2009 6:30 pm ]
Post subject:  RE:Elimination of multiples - Sieve of Eratosthenes

That's a good question. What are your thoughts about it?

Author:  nba [ Mon Jun 08, 2009 6:44 pm ]
Post subject:  RE:Elimination of multiples - Sieve of Eratosthenes

hmm im guessing it shud go after Razz
for i : 2 .. n


: