Results 1 to 5 of 5
  1. #1
    loja11 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Question 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);

    }
    }

  2. #2
    mityay is offline Member
    Join Date
    Jun 2009
    Posts
    8
    Rep Power
    0

    Default

    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
    Last edited by mityay; 01-05-2011 at 05:20 AM.

  3. #3
    loja11 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    mityay:

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by loja11 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    k_nica is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    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

  1. displaying prime number
    By eng_hyzoom in forum New To Java
    Replies: 1
    Last Post: 11-26-2010, 11:21 AM
  2. Prime Number - true , false
    By pinkdreammsss in forum Java Applets
    Replies: 11
    Last Post: 05-04-2010, 02:49 PM
  3. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 11:42 PM
  4. Replies: 10
    Last Post: 02-20-2010, 08:25 PM
  5. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •