posted 7 months ago

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?

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?

DRAGON SWALLOWED HIM... END OF STORY

posted 7 months ago

- 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)

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

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

posted 7 months ago

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..

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..

DRAGON SWALLOWED HIM... END OF STORY

Campbell Ritchie

Marshal

Posts: 56600

172

posted 7 months ago

- 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.

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 7 months ago

Yes I did, I made it without the break and I also chose not to include

Campbell Ritchie wrote:Welcome to the Ranch

When you sort out the other problems, I challenge you to rewrite your loops so as to avoidbreak. Also, maybe those of us more experienced could find a solution with a

Why did you choose that type forisPrime?

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..

DRAGON SWALLOWED HIM... END OF STORY

posted 7 months ago

Edit : I chose an integer to work as a flag.

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 avoidbreak. Also, maybe those of us more experienced could find a solution with a

Why did you choose that type forisPrime?

Yes I did, I made it without the break and I also chose not to includeisPrimeas 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.

DRAGON SWALLOWED HIM... END OF STORY

posted 7 months ago

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

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

DRAGON SWALLOWED HIM... END OF STORY

posted 7 months ago
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

- 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:

>

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:

>