# Prime #

• 10-11-2002, 02:49 PM
slybitz
Prime #
Hi! My teacher put this java code up for us. It makes it so that you can find out if a # is a prime # or not. Can someone help me with what exactly the code means and also a way to have the user enter a number? Or to come up with another simple algorithm to find a prime #. Thank you soooooo much! here's the code:
<CODE>
class prime
{
public static void main (String args[])
{
Integer arg1;
int[] primes;
int next_case = 7;
boolean prime;
int next_increment = 4;
int lv_index = 3;
int sq_index = 1;
int square = 9;
int a;
arg1 = new Integer(args[0]);
primes = new int[arg1.intValue()];
primes[0] = 2;
primes[1] = 3;
primes[2] = 5;
primes[3] = 7;
while (lv_index < arg1.intValue())
{
next_case += next_increment;
next_increment = 6 - next_increment;
if (next_case >= square)
{
sq_index++;
square = primes[sq_index] * primes[sq_index];
}

prime = true;
for (a = 2; a < sq_index; a++)
{
if ( (next_case % primes[a]) == 0)
{
prime = false;
break;
}
}
if (prime == true)
{
lv_index++;
if (lv_index == arg1.intValue())
break;
primes[lv_index] = next_case;
}
}
for (a = 0; a < primes.length; a++)
{
System.out.println(a+1 + ": " + primes[a]);
}
}
}
</CODE>