This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Prime No. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Prime No." Watch "Prime No." New topic

Prime No.

Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
Hi, I want to find out 10001st prime number of the site Projecteuler, and here is my programme

and I'm getting the answer as 104149. I thougth I've written my code correctly, but yet I'm getting wrong answer. Could anyone please tell me the reason of not having a right result? A small hint is enougth.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41073
What does isPrime(25) return?

Ping & DNS - my free Android networking tools app
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37941
When you get prime(25) to return true, get a pencil and paper, and write 1 2 3 4 . . . 24 25. Then go through your method (a complicated bit of programming, there) and work out which numbers it will reject. You need to count the iterations of the loop carefully.

Then find out how the "Sieve of Eratosthenes" works. Far better way of working out prime numbers.
I agree. Here's the link:
subject: Prime No.
Similar Threads
Finding the Nth Prime Number
Putting prime numbers into an array
Need help with Prime number Loop and with boolean statement
Program that takes an int and returns an array of all the prime numbers smaller than that int
counting and primes