Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

Prime numbers (Eratosthenes)

Thread view: 
JS - 29 Dec 2004 21:05 GMT
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 - 29 Dec 2004 21:12 GMT
> I need to implement the following algorithm where P needs to be
> modelled af an array of booleans ( booleans[]P = new boolean[n + 1]).
[quoted text clipped - 5 lines]
> //{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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.