• Post Reply Bookmark Topic Watch Topic
  • New Topic

Prime number program  RSS feed

 
Mishra Saurabh
Greenhorn
Posts: 24
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 24
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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..
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 24
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 24
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 24
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 24
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 24
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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:
>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!