Nintendo World Report Forums

Community Forums => General Chat => Topic started by: Xavya on October 10, 2006, 05:21:35 PM

Title: Anyone Know Java? lol
Post by: Xavya on October 10, 2006, 05:21:35 PM
Hey guys I need Java help lol. I would REALLY REALLY appreicate some help, yes im a desperate sad man for all purposes.... Basically I need a relatively simplep rogram that finds the first 100 prime numbers. I would appreciate any help i could get especailly in regards to the algorithm for finding the primes. I mean like Java is similar to C++ Im just having some difficulty with an algorithm to determine prime numbers. Thanks, any and all help would be appreciated. Thanks, Xavya

PS ALso i would love you for all eternity thanks
Title: RE: Anyone Know Java? lol
Post by: Smoke39 on October 10, 2006, 05:57:18 PM
Okay, here's an idea:

Have an integer that keeps track of how many prime numbers you've found.  Set up a while loop that goes until you've found 100 prime numbers.

Have a floating point that you increment by 1 on each iteration of the loop.  This will be the current number you're checking the primeness of.

Have another counter for a nested loop.  Initialize it to 2, and have the loop go until it reaches the possibly prime number(PPN from now on) - 1.

Within the nested loop, divide the PPN by the counter.  If PPN/counter == int(PPN/counter), then you know that the PPN is divisible by the counter (ie, not prime) and you can break out of the loop.

After the nested loop, check if the counter reached PPN.  If so, then it made it through the nested loop without breaking.  That means the number's prime.  Print it to the screen, or store it (whichever you're supposed to do), and increment your prime counter.


I think that'd work.  No idea if that's really the most efficient way, though.
Title: RE: Anyone Know Java? lol
Post by: Athrun Zala on October 10, 2006, 06:13:04 PM
I'd guess you aren't limited by memory if you should ask for such algorithm

so, I'd say the following:
- have a variable "number" which begins at 1 (you could begin at 2 actually...) and is the number you'll verify whether it's prime or not
- create an array (or list or whatever floats your boat ^^) with as many spaces as the numbers you need (100 in this case)
- check if "number" is prime or not (reminder: prime number is one who's only divisors are 1 and itself)...if it is, save it in the array (at the last empty slot)
- increment "number" until the array is filled (ie, you have 100 prime numbers)

EDIT: awww....beaten (seems like we had similar ideas....)
Title: RE: Anyone Know Java? lol
Post by: Nick DiMola on October 10, 2006, 08:14:17 PM
Nice call on the code guys. I got bored so I wrote the program for fun (I know I'm a loser), plus it felt good to tear through something so rudimentary. Check it out here. Hope that helps Xavya.
Title: RE: Anyone Know Java? lol
Post by: couchmonkey on October 11, 2006, 06:40:04 AM
Nerds!  (I'm just jealous because someone already answered it before I got here).
Title: RE: Anyone Know Java? lol
Post by: Ceric on October 11, 2006, 06:51:15 AM
You know isn't there an algorithm that involves doubling and either adding or subtracting 1?
Title: RE:Anyone Know Java? lol
Post by: UltimatePartyBear on October 11, 2006, 06:57:41 AM
You can take some shortcuts.  For example, 2 is the only even prime.  Treat it as a special case, start with 3, and increment the possible prime by 2 every time.  You can start your checks with n % 3 instead of n % 2 (% is the modulus operator, which returns the remainder of an integer division, for those who are confused).  Of course, if this is a homework assignment, you may be docked points for hard coding 2 like this.  If, on the other hand, you just want to find primes, this step will save some time.

It isn't necessary to check to see if the possible prime is evenly divisible by every number less than itself.  It's impossible for a number n to be evenly divisble by a number greater than n/2.  Also, since you're skipping all the even numbers in the first place, you don't have to check past n/3.  It may be possible to trim down the possibilites more if you put more thought into it.

This is right off the top of my head, so I may have overlooked something: