1

I have to find the 10001st prime number which is 104753 but when I run my code I get 104754.

I need help finding the problem. What can I change so it finds the 10001st prime number ? Thanks

This is what I've done so far :

public class Prime
{

    public static void main(String[] args)
    {
        int a = 1;
        int primes = 0;

        while (primes < 10001)
        {
            if (isPrime(a) == true)
            {
                primes++;
            }

            a++;
        }

        System.out.println("The 10001st prime number is " + a);
    }

    public static boolean isPrime(int b)
    {
        boolean x = false;
        int counter = 0;

        for (int i=1; i<=b; i++)
        {
            if (b%i == 0)
            {
                counter++;
            }

            if (counter == 2 && i == b)
            {
                x = true;
            }
        }

        return x;
    }
}
Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
  • 8
    Welcome to Stack Overflow! Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, by tracing the progress of your program, and comparing it to what you expect to happen. As soon as the two diverge, then you've found your problem. (And then if necessary, you should construct a [minimal test-case](http://sscce.org).) – Oliver Charlesworth Nov 15 '13 at 14:57
  • 2
    Please use proper indentation, it makes your code that much more readable! – Dragondraikk Nov 15 '13 at 14:57
  • Which prime is incorrect? I suggest you print out your primes and see where it is different to the known list of primes. BTW You can make your `isPrime` *much* more efficient. – Peter Lawrey Nov 15 '13 at 14:58
  • Just a sidenote: A faster implementation to find primes is the [Sieve of Eratosthenes](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes). – Sirko Nov 15 '13 at 14:59
  • 3
    Please use proper variable names, especially for things that provide responses. `int a` and `boolean x` are very vague and require working backwards to figure out what they mean (in this case index, and whether or not isPrime is true) – Compass Nov 15 '13 at 15:00
  • By the way, you might want to take a look at this very easy to implement optimization for determining if a number is prime. http://stackoverflow.com/questions/5811151/why-do-we-check-upto-the-square-root-of-a-prime-number-to-determine-if-it-is-pri – cangrejo Nov 15 '13 at 15:06

4 Answers4

4

Right after you find the number, you increment it.

if(isPrime(a) == true)
{
    primes++;
}
    a++;

You should print it before incrementing it.

cangrejo
  • 2,189
  • 3
  • 24
  • 31
  • Printing it before you increment it means that you need to check twice for the exit condition `if (primes == 10001)`. Which is in general not such a good technique. – nelly Nov 18 '13 at 10:47
  • @nelly That's right. It's not the best choice, given that he only wants to print that one prime. I wasn't taking that into account. – cangrejo Nov 18 '13 at 12:39
2

You increment 'a' after determining whether it is prime.

If this were my project I would start with:

a = 0;

and then in my loop I would:

a++;
if (isPrime(a)) {
    count++;
}
rolfl
  • 17,539
  • 7
  • 42
  • 76
2

One simple solution:

int a = 0;
int primes = 0;
while(primes < 10001) {
    a++;
    if(isPrime(a) == true) {
        primes++;
    }
}

Of course there are many others. Perhaps your should also consider faster algorithms that find prime numbers:

http://www.wikihow.com/Check-if-a-Number-Is-Prime

And as a start, make sure to exit early in your isPrime test method: If you reach a point during the loop where your counter is higher than 2, you can exit. And then, you don't really need to test for i=0.

nelly
  • 417
  • 3
  • 9
1

An extra a++ is done in the while - loop, when prime == 10001.

 while (primes < 10001) {
        if (isPrime(a)) {
            primes++;
        }
        a++;
 }

You can use --a to print the prime you want.

System.out.println("The 10001st prime number is " + (--a));
Mengjun
  • 3,159
  • 1
  • 15
  • 21