For Programmers: Free Programming Magazines  


Home > Archive > Java Help > December 2004 > Prime numbers (Eratosthenes)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Prime numbers (Eratosthenes)
JS

2004-12-29, 8:58 pm


I need to implement the following algorithm where P needs to be modelled af
an array of booleans ( booleans[]P = new boolean[n + 1]).

But how do I write this in the {} thats gonna be executed as long as 2*i<=
n??

for (i = 2; 2*i <=n; i = i + 1)
//{remove from P all multiples of i};
//print the contents of P




Stefan Schulz

2004-12-29, 8:58 pm

On Wed, 29 Dec 2004 22:05:42 +0100
"JS" <dsa.@asdf.com> wrote:

>
> I need to implement the following algorithm where P needs to be
> modelled af an array of booleans ( booleans[]P = new boolean[n + 1]).
>
> But how do I write this in the {} thats gonna be executed as long as
> 2*i<= n??
>
> for (i = 2; 2*i <=n; i = i + 1)
> //{remove from P all multiples of i};
> //print the contents of P


First, you might wish to reconsider the loop length. It will be
sufficient to mark all multiples of primes less then the square root of
n. Then you iterate over all multiples of the current i smaller then
(or equal to) n, marking each such multiple as false.

What, you wanted me to do your homework for you? :-P Its all written
down there, and i bet in your textbook as well.

--
In pioneer days they used oxen for heavy pulling, and when one ox
couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
be trying for bigger computers, but for more systems of computers.
--- Rear Admiral Grace Murray Hopper
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com