Results 1 to 5 of 5
Thread: find a prime number
 01022011, 10:57 AM #1Member
 Join Date
 Jan 2011
 Posts
 18
 Rep Power
 0
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);
}
}
 01052011, 06:18 AM #2Member
 Join Date
 Jun 2009
 Posts
 8
 Rep Power
 0
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 nonprime 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) whilecontinue statement isn't used correctly. With your implementation, while loop keeps running from while to continue indefinitely and never endsLast edited by mityay; 01052011 at 06:20 AM.
 01092011, 07:14 PM #3Member
 Join Date
 Jan 2011
 Posts
 18
 Rep Power
 0
mityay:
thank you for the comments on my code and suggestions regarding under which heading to post such questions.
 01092011, 07:33 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,190
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 02172011, 09:54 AM #5Member
 Join Date
 Feb 2011
 Posts
 2
 Rep Power
 0
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.");
int primeN = IO.readInt();
if(primeN == 0)
{
IO.reportBadInput();
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);
IO.outputIntAnswer(finalPrime);
}
}
}}}
Similar Threads

displaying prime number
By eng_hyzoom in forum New To JavaReplies: 1Last Post: 11262010, 12:21 PM 
Prime Number  true , false
By pinkdreammsss in forum Java AppletsReplies: 11Last Post: 05042010, 02:49 PM 
Finding nth prime number
By dextr in forum New To JavaReplies: 2Last Post: 04122010, 11:42 PM 
How to Determine prime number and making shape using loop?
By cyzash in forum New To JavaReplies: 10Last Post: 02202010, 09:25 PM 
Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 01:50 AM
Bookmarks