• 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

Prime number program

 
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I made this program in java to print the number of prime numbers between a range. I tested it for the range 1-100 but couldn't get the right answer. It gives the output 23 prime no. between the range I mentioned, the output should be 25. Below is the code.

Am I missing something?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could add some print statements in order to see what numbers your program thinks is prime (or not).
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Your program treats '1' as prime. (among other problems)
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Your program treats '1' as prime. (among other problems)


Thanks for your reply. But my program doesn't treat 1 as a prime number.
I added a print statement, what I found was that my program doesn't consider 2,3 and 5 prime number. It starts with number 7. I looked it up in some tutorials,but their code is similar to mine.
I am not able to understand the problem..
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with line 13, the j loop, and when it terminates.  But there's other things too, such as how the isPrime variable is set.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

When you sort out the other problems, I challenge you to rewrite your loops so as to avoid break. Also, maybe those of us more experienced could find a solution with a
Why did you choose that type for isPrime?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what purpose does count serve?  you set it to 1 initially.  But if the range is 8-9, there are no primes between, but you've already counted one.

Is the range inclusive or exclusive?

It should be pretty obvious why 2 isn't considered prime. You default to "false". if your lower limit is 2, then your inner loop is from j=2 to (2/2) or 1, so the loop never executes, and so isPrime is never set to true.
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:It has to do with line 13, the j loop, and when it terminates.  But there's other things too, such as how the isPrime variable is set.


Thanks.. I appreciate it.
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

When you sort out the other problems, I challenge you to rewrite your loops so as to avoid break. Also, maybe those of us more experienced could find a solution with a
Why did you choose that type for isPrime?


Yes I did, I made it without the break and I also chose not to include isPrime as a boolean, instead I chose an integer to work as an integer that I declared inside the first loop. It works fine now..
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mishra Saurabh wrote:

Campbell Ritchie wrote:Welcome to the Ranch

When you sort out the other problems, I challenge you to rewrite your loops so as to avoid break. Also, maybe those of us more experienced could find a solution with a
Why did you choose that type for isPrime?


Yes I did, I made it without the break and I also chose not to include isPrime as a boolean, instead I chose an integer to work as an integer that I declared inside the first loop. It works fine now..



Edit :  I chose an integer to work as a flag.
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:what purpose does count serve?  you set it to 1 initially.  But if the range is 8-9, there are no primes between, but you've already counted one.

Is the range inclusive or exclusive?

It should be pretty obvious why 2 isn't considered prime. You default to "false". if your lower limit is 2, then your inner loop is from j=2 to (2/2) or 1, so the loop never executes, and so isPrime is never set to true.


count is used to count the number of prime numbers between the range given. I initially set it at 0, but when the answers did not match I changed it to 1 to see what happens. the range is inclusive.
Yes it is obvious about 2, 3 and 5. I tweaked my code a bit. Now it gives correct output
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code works fine, as you can see I chose against break on you suggestion, but can you please explain why did you tell me to opt against break?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've done away with the optimization that meant you only had to test half of the values.

If you had wanted to take this optimization to the next step, you'd only have to check up to the square root of the number, as in:
>
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic