find a prime number

• 01-02-2011, 10:57 AM
loja11
find a prime number
Hi

I am stuck on this code. I am trying to find the 10001 prime number.
Any help with my code would be appreciated.

public class TenThousOnePrime {
static double prime;

public static void main(String[] args){

double testOne = 4;
int counter = 2;

for (int i = 2; i < testOne; i++){
if (testOne % i == 0){
testOne++;
i = 2;
} else {

prime = testOne;
testOne++;
counter++;

}
while (testOne < 10002)
continue;
}

System.out.println(prime);

}
}
• 01-05-2011, 06:18 AM
mityay
Hi loja11,
Some suggestions regarding the posting:

1) The issue doesn't seem to be related to MultiThreading. I would place it into "New to Java" category
2) In order to make your code look like code and be more readable, it needs to be surrounded by [code] identifiers
3) You have two variables of type double. I don't see any reason of using them for finding prime number as it includes only integers
4) Your logic of determining the non-prime number seems to be on the right path. You check all the numbers from 2 to testOne and if at least one of them divides testOne, testOne is discarded and new possible prime is tested by incrementing testOne. However, else block is on the wrong location. Counter must be incremented after you have found that the prime number is found (i.e. after the loop), not within the loop
5) while-continue statement isn't used correctly. With your implementation, while loop keeps running from while to continue indefinitely and never ends
• 01-09-2011, 07:14 PM
loja11
mityay:

thank you for the comments on my code and suggestions regarding under which heading to post such questions.
• 01-09-2011, 07:33 PM
JosAH
Quote:

Originally Posted by loja11
mityay:

thank you for the comments on my code and suggestions regarding under which heading to post such questions.

I suggest a sieve of Eratosthenes for this purpose; your method starts all over with possible divisors, the sieve method doesn't do that. Once you've found the 10001th bit set you've found the prime number you're looking for. Also have a look at the BitSet class; it can serve as the sieve itself.

kind regards,

Jos
• 02-17-2011, 09:54 AM
k_nica
You'll have to change to what ever method you use to input or output a value. I'm using IO.java.
Other than that, works like a charm.

{{{

public class NthPrime
{
public static void main(String[]args)
{
int count=0;
int numTest=1;
int finalPrime=0;

System.out.println("Enter a value 'N' to find the 'Nth' prime number.");

if(primeN == 0)
{
return;
}

do
{
double limit = Math.sqrt(numTest);
boolean prime = true;

if(numTest==1)
{
prime = false;
}

else if(limit<2)
{
prime=true;
}

else
{
for(int divisor = 2; divisor <= limit && prime; divisor++)
{
if(numTest % divisor == 0)
{
prime = false;
break;
}
/*
else
{
prime=true;
finalPrime = numTest;
}
*/
}
}

if(prime==true)
{
count=count+1;
finalPrime=numTest;
numTest++;
System.out.println("The current prime number stored is: " + finalPrime);
}
else
{
numTest++;
}

}while(count<primeN);