
Cyran Meriel wrote:I found this solution online and it works but I cant understand the logic behind it..I understand that the break is important but still... i and j increases by the same amount and are always equal according to the statements. So for every loop j is equal to i so it should print 2,3,4,5,6 etc..
It says when j == i print i.. It cant jump over that statement to check if i%j == 0 and then go back to the print. Its not put in the right order to do that..
Henry Wong wrote:
Cyran Meriel wrote:I found this solution online and it works but I cant understand the logic behind it..I understand that the break is important but still... i and j increases by the same amount and are always equal according to the statements. So for every loop j is equal to i so it should print 2,3,4,5,6 etc..
It says when j == i print i.. It cant jump over that statement to check if i%j == 0 and then go back to the print. Its not put in the right order to do that..
One is an inner loop to the other. They don't iterate together. In fact, for one iteration of the outer loop, the inner loop has to start from the beginning (2) until the end (or until it breaks).
Henry
Campbell Ritchie wrote:That is an inefficient way to find prime numbers. You do not notice the inefficiency because you are only searching up to 100.
The inner loop does not need to go up to i.
Find out about the Sieve of Eratosthenes which is a more efficient way to seek prime numbers.
Cyran Meriel wrote:
Where does it say that the inner loop has to start from the beginning after one iteration of the outer loop? I mean it says j++, so it has to add 1 for every loop right? So then it will increase, how can it reset as 2 then?
Henry Wong wrote:
In order to do the next iteration of the loop, the previous iteration has to complete (which includes break)...
Henry
Cyran Meriel wrote:I think I get it now, I had to write it in this way in order for it to make sense to myself. Please tell me if I got anything wrong.
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Nope, I think you've got it spot on. And I hate to say, but you'll have to do that sort of analysis any time you don't understand what's going on in a program.
Cyran Meriel wrote:It finds prime by zeroing in on numbers that are not divided evenly by 2. If they do they are not prime...
The problem I see in this program is that it's not effecting since it will check numbers that we already declared as primes.
But to figure out a way thats more effective is too hard for me...
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Could you hold this puppy for a sec? I need to adjust this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
