PDA

View Full Version : Who Knows Java? lol



Xavya
October 11th, 2006, 03:49
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

Jeremysr
October 11th, 2006, 04:50
Here's my prime number PHP function I wrote a while ago. I don't know Java but it looks like the syntax is almost exactly the same.


function prime($min, $max) {
if ($min % 2 == 0) { $min++; }
if ($max % 2 == 0) { $max--; }
for ($x = $min; $x <= $max; $x += 2) {
$n = $x;
$lim = $x;
while ($lim > $n) {
if ($x % $n == 0) { $blah = 1; }
$lim = $x / $n;
$n += 2;
}
if ($blah == 0) { $prime_numbers .= "$x "; }
}
return $prime_numbers;
}

For the first hundred you'd do this:

$numbers = prime(1, 100)

But instead of .= for string concatenation you'd use + and there might be some other things to change around.