• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt regarding finding prime number program  RSS feed

 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought a Sieve of Eratosthenes would be a better algorithm.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…but it calculates a block of prime numbers in advance.
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!