• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How many prime Exist between two numbers?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to count, how many prime numbers exist, but it give me wrong answer: Exactly 1254 prime numbers exist between 1 and 10,000.

can you tell me why? Thank you!




Result:

Exactly 1254 prime numbers exist between 1 and 10,000.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niloo Rad wrote:something is wrong with my code and i can't figure it out! will you help me to find it?


Well, you haven't given us all of it. Where is MAX_PRIME defined? Without that and your isPrime() method, it's a bit tough to work out exactly what's wrong.

However, there is one simple optimization: you DON'T have to check every number.
In fact, (tip) ALL primes greater than 3 are equal to 6k ± 1, where k is a positive integer.

Winston
 
Niloo Rad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JUST UPDATED MY CODE
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niloo Rad wrote:JUST UPDATED MY CODE



Don’t That makes Winston’s reply look nonsense. It means nobody can work out the changes. Please repost the original code and a new post with the changes. And read this.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this...

[complete solution removed]
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niloo Rad wrote:JUST UPDATED MY CODE


And I'm afraid it's still wrong. What does isPrime(49), or indeed isPrime(9), return?

Winston
 
Roger Wolfenden
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Winston would you like to elaborate on what is wrong with the code?
  • isPrime(9) = 29
  • isPrime(49) = 229


  • Interesting too, I notice that my code has been removed by @Winston. I'm just wondering why. Have I broken some code of etiquette?

    [Edit by Winston] See below.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roger Wolfenden wrote:How about this...


    Hi Roger.

    I'm sure your motives were great, but we don't like people to post ready-made solutions here - particularly in the 'Beginners' forum.
    It's much better to guide someone to their own solution. Please read the NotACodeMill (←click) page.

    I've kept your original post, and if my colleagues ask me to, I'll restore it.

    Thanks.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roger Wolfenden wrote:@Winston would you like to elaborate on what is wrong with the code?

  • isPrime(9) = 29
  • isPrime(49) = 229

  • I was referring to Niloo's code, not yours, and specifically to his isPrime() method.

    Winston
     
    Roger Wolfenden
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OK @Winston understand and accept your logic.

    FYI, I too am a newbie but quite liked the, "Let's find all the primes between 0 and 1000 challenge." Hence my code.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roger Wolfenden wrote:OK @Winston understand and accept your logic.


    No probs. And please, feel free to help. We just don't like beginners to be spoon-fed.

    Winston
     
    Bartender
    Posts: 1111
    Eclipse IDE Oracle VI Editor
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so you pass in 3 numbers assign them to variables with very bad names a,b,c so we have no idea what you intended to do with them, you then do nothing with them.

    the controlling loop in countPrimes always goes from 2 to MAX_PRIME.

    this is just a nice layout thing...
    you do not need to indent the validation try catch blocks within each other



     
    Campbell Ritchie
    Marshal
    Posts: 80874
    506
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This sounds like a project Euler question. Is it?
    I would suggest you create an array and use a sieve of Eratosthenes to work out whether each number is prime. Then count the primes. 0 and 1 don’t count. Much more effecient than an isPrime method.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic