Results 1 to 5 of 5
Thread: find a prime number
- 01-02-2011, 09:57 AM #1
Member
- 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);
}
}
- 01-05-2011, 05:18 AM #2
Member
- 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 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 endsLast edited by mityay; 01-05-2011 at 05:20 AM.
- 01-09-2011, 06:14 PM #3
Member
- 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.
- 01-09-2011, 06:33 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,412
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-17-2011, 08:54 AM #5
Member
- 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: 11-26-2010, 11:21 AM -
Prime Number - true , false
By pinkdreammsss in forum Java AppletsReplies: 11Last Post: 05-04-2010, 02:49 PM -
Finding nth prime number
By dextr in forum New To JavaReplies: 2Last Post: 04-12-2010, 11:42 PM -
How to Determine prime number and making shape using loop?
By cyzash in forum New To JavaReplies: 10Last Post: 02-20-2010, 08:25 PM -
Prime Number - System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04-26-2009, 01:50 AM


LinkBack URL
About LinkBacks


Bookmarks