• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

doubt regarding finding prime number program

 
Ranch Hand
Posts: 94
Eclipse IDE Oracle Java
  • 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]
 
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)
 
Marshal
Posts: 65113
247
  • 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: 65113
247
  • 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 Oracle Java
  • 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?

 
Bartender
Posts: 10775
71
Hibernate Eclipse IDE 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 Oracle Java
  • 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 Oracle Java
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!