-
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>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks