programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Prime number program

Mishra Saurabh
Greenhorn
Posts: 24

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?

Carey Brown
Saloon Keeper
Posts: 3329
46
• 1
You could add some print statements in order to see what numbers your program thinks is prime (or not).

Carey Brown
Saloon Keeper
Posts: 3329
46
• 1
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
Greenhorn
Posts: 24
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..

Knute Snortum
Sheriff
Posts: 4289
127
• 1
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.

Campbell Ritchie
Marshal
Posts: 56600
172
• 1
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?

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
• 1
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
Greenhorn
Posts: 24
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
Greenhorn
Posts: 24
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
Greenhorn
Posts: 24
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
Greenhorn
Posts: 24
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
Greenhorn
Posts: 24

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: 3329
46
• 1
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:
>