 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
JS Guest
|
Posted: Wed Dec 29, 2004 9:05 pm Post subject: Prime numbers (Eratosthenes) |
|
|
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
|
|
| Back to top |
|
 |
Stefan Schulz Guest
|
Posted: Wed Dec 29, 2004 9:12 pm Post subject: Re: Prime numbers (Eratosthenes) |
|
|
On Wed, 29 Dec 2004 22:05:42 +0100
"JS" <dsa. (AT) asdf (DOT) com> wrote:
| Quote: |
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? 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|