• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding n th prime number

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic