Author Topic: Anyone Know Java? lol  (Read 2102 times)

0 Members and 1 Guest are viewing this topic.

Offline Xavya

  • Score: 0
    • View Profile
Anyone Know Java? lol
« 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
UMmmmmmm HI!

Offline Smoke39

  • Smoking is only bad for you if you're not made of smoke already
  • Score: 3
    • View Profile
RE: Anyone Know Java? lol
« Reply #1 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.
GOREGASM!

Offline Athrun Zala

  • Tween Idol
  • Score: 4
    • View Profile
    • TM!
RE: Anyone Know Java? lol
« Reply #2 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....)
Quote from: [b]Professional 666[/b]
JOIN MY ASS

IT'LL BE LOTS OF FUN
Best. Quote. Ever. XD

Offline Nick DiMola

  • Staff Alumnus
  • Score: 20
    • View Profile
    • PixlBit
RE: Anyone Know Java? lol
« Reply #3 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.
Check out PixlBit!

Offline couchmonkey

  • I tye dyed my Wii and I love it
  • Score: 2
    • View Profile
RE: Anyone Know Java? lol
« Reply #4 on: October 11, 2006, 06:40:04 AM »
Nerds!  (I'm just jealous because someone already answered it before I got here).
That's my opinion, not yours.
Now Playing: The Adventures of Link, Super Street Fighter 4, Dragon Quest IX

Offline Ceric

  • Once killed four Deviljho in one hunt
  • Score: 0
    • View Profile
RE: Anyone Know Java? lol
« Reply #5 on: October 11, 2006, 06:51:15 AM »
You know isn't there an algorithm that involves doubling and either adding or subtracting 1?
Need a Personal NonCitizen-Magical-Elf-Boy-Child-Game-Abused-King-Kratos-Play-Thing Crimm Unmaker-of-Worlds-Hunter-Of-Boxes
so, I don't have to edit as Much.

Offline UltimatePartyBear

  • Voice of Reason
  • Score: 35
    • View Profile
RE:Anyone Know Java? lol
« Reply #6 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: