Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prime No.

 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does isPrime(25) return?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic