• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding n th prime number

 
rajarshi roy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a code for a program that will find the n th prime number.As for example 7 is the 4th prime number,11 is the 5th.
The method will take argument as n,and it will find the nth prime number.But I am facing some difficulty with the code.The code doesnt terminate by itself and doesnt return the desired value.Someone please help me out.
 
Dennis Grimbergen
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To loop till Integer.MAX_VALUE takes a while I guess. Besides that, this looping is also in a while loop. That calculation would takes ages, and that's why it doesn't stop by itself.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You loop from 0 to Integer.MAX_VALUE without checking the count. You only re-check count after this loop. Merge both loops into one:
This will still loop over i, but this single loop will stop once count == n.
 
rajarshi roy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You loop from 0 to Integer.MAX_VALUE without checking the count. You only re-check count after this loop. Merge both loops into one:
This will still loop over i, but this single loop will stop once count == n.

I modified the code to take into account what you said..
The code compiles,runs and gives the desired output.But I am still facing one odd problem.
I am using Eclipse IDE for coding.Whenever I am trying to run the code,a dialogue box appears that says "Error exists in required project(s).......Proceed with launch?"
What does this mean??
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Means there's an error in your current project (or a required one, or something) that doesn't affect the running of the code you're using, or that it does and you've been running the same version of the app without realizing it. Open up the "Problems" tab (view) and see what's there.
 
Karthik Shiraly
Bartender
Posts: 1208
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've a comment to make about the ifPrime check: To check divisibility of x, it's not needed to test all numbers from {2 to x} as factors; it's enough to test from {2 to squareroot(x) inclusive}. Take any pair of factors of x, which yield x on multiplication - one of the pair will always be less than or equal to squareroot(x).

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic