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
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# doubt regarding finding prime number program

Ranch Hand
Posts: 94
• Number of slices to send:
Optional 'thank-you' note:
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

[Added code tags - please read UseCodeTags for details]

Bartender
Posts: 4568
9
• Number of slices to send:
Optional 'thank-you' note:
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: 76885
366
• Number of slices to send:
Optional 'thank-you' note:
I would have thought a Sieve of Eratosthenes would be a better algorithm.

Campbell Ritchie
Marshal
Posts: 76885
366
• Number of slices to send:
Optional 'thank-you' note:
…but it calculates a block of prime numbers in advance.

kiran kumar reddy
Ranch Hand
Posts: 94
• Number of slices to send:
Optional 'thank-you' note:

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: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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.

 Everybody! Do the Funky Monkey! Like this tiny ad! the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising
reply
Similar Threads