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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Paul Clapham
• Devaka Cooray
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Knute Snortum
• Liutauras Vilda
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Piet Souris
Bartenders:
• salvin francis
• Carey Brown
• Frits Walraven

# doubt regarding finding prime number program

Ranch Hand
Posts: 94
here is my program
according to my understanding after n=scan.nextInt() for loop is started and inside for loop there is a condition which says i<=n/2. . when the condition is i<=n/2 and when program is compiled it asks for number to input when i gave an input it is giving correct output. But my doubt is when i change that condition to i<=n and compile it even though i gave a prime number it says " it is not a prime number". whats the difference between condition i<=n/2 and i<=n condition. please help me in this regard

Bartender
Posts: 4568
9
i < n would work. But if you set the condition to i <= n, what's going to happen when i = n?

(You don't even have to go as far as n/2 to work out if it's prime - think about it)

Marshal
Posts: 68853
275
I would have thought a Sieve of Eratosthenes would be a better algorithm.

Campbell Ritchie
Marshal
Posts: 68853
275
…but it calculates a block of prime numbers in advance.

kiran kumar reddy
Ranch Hand
Posts: 94

Matthew Brown wrote:i < n would work. But if you set the condition to i <= n, what's going to happen when i = n?

(You don't even have to go as far as n/2 to work out if it's prime - think about it)

yes i<n is working fine. but what is difference between i<=n and i<=n/2 and why it showing different output?

Bartender
Posts: 10777
71

kumar reddyss wrote:yes i<n is working fine. but what is difference between i<=n and i<=n/2 and why it showing different output?

1. The difference is the upper limit.
2. For exactly the reason that Matthew said, but you have to think about it. Hint: how are you checking if i is prime?

Winston

kiran kumar reddy
Ranch Hand
Posts: 94

Matthew Brown wrote:i < n would work. But if you set the condition to i <= n, what's going to happen when i = n?

(You don't even have to go as far as n/2 to work out if it's prime - think about it)

when i=n the condition will be true and as usual it enters in to the for loop and it will execute res=n%i and so on..

what ever the condition either i<=n and i<=n/2 it will be true and will go in to for loop and whats the difference is there output.

i am raising these doubts according to my knowledge.

Matthew Brown
Bartender
Posts: 4568
9
• 1

kumar reddyss wrote:when i=n the condition will be true and as usual it enters in to the for loop and it will execute res=n%i and so on..

And what is the value of n % i, when i = n?

kiran kumar reddy
Ranch Hand
Posts: 94

Matthew Brown wrote:

kumar reddyss wrote:when i=n the condition will be true and as usual it enters in to the for loop and it will execute res=n%i and so on..

And what is the value of n % i, when i = n?

yes, thank you i got it now.

 WARNING! Do not activate jet boots indoors or you will see a tiny ad: Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java