AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Prime numbers (Eratosthenes)

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
JS
Guest





PostPosted: Wed Dec 29, 2004 9:05 pm    Post subject: Prime numbers (Eratosthenes) Reply with 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




Back to top
Stefan Schulz
Guest





PostPosted: Wed Dec 29, 2004 9:12 pm    Post subject: Re: Prime numbers (Eratosthenes) Reply with quote



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? Razz 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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.