Prime numbers consfusion

Can someone explain me the following code:

1
2
3
4
5
6
7
8
9
int i, j;
 
for(i=2;i<1000;i++){
    for(j=2;i/j;j++){
        if(!(i%j)) break;
    if(j>(i/j)) cout<<i<<" is prime\n";
    }
 
}


The last line/if is the reason of my confusion; how does it decide what num is prime and when?
1
2
3
4
5
6
7
8
9
10
int i, j;
 
for( i=2; i < 1000; i++) // Iterates through the possible primes from 2 to 1000
{
    for( j=2; i/j; j++) // Iterates through possible divisors for all i
    {
        if(!(i%j)) break; // If i mod j is NOT 0 (if it is possible to divide i by j without getting decimals), then the number is a prime
        if(j>(i/j)) cout<<i<<" is prime\n"; // if j has iterated far enough to conclude that after this point there are no possible divisors to divide i into an integer value, it is a prime
    }
}

We are basically concluding that you CANNOT have higher divisors j for i above i/j, and if it has gone as far to check that, we can conclude that up to that point they have no subdivisors and are therefor primes.

EXAMPLE:
j = 2
17/2 = 8.5
j = 3
17/3 = 5 + 2/3
j = 4
17/4 = 4 + 1/4
j = 5
5 > 17 / 5
5 > 3 + 2/5
After this point all subdivisors will give decimals in their results, even for non-primes.
I made some typo; here is the original condition

1
2
3
4
5
6
7
for(i=2;i<100;i++){
		for(j=2;j<=(i/j);j++){
			if(!(i%j)) break;
		if(j>(i/j)) cout<<i<<" is prime\n";
		}
		
	}



One more thing; subdivisor 4 is not held as prime beacuse it's tossed by the break if condition?
Last edited on
neither of these return a value let alone a prime.
Topic archived. No new replies allowed.