Your loop is an endless loop. The first factor you found is 1 (since num % 1 - 0 ), and so you divide num by 1 , which leads to num , which returns to the for loop, which does the same thing over and over again.
Also with this fixed (initialize i in a loop with 2 ), your inner for loop is most likely an infinite loop and / or calls UB. Otherwise (as others have argued) this βjustβ works for a very long time. For the case when it is different (provided that most common platforms are here). It depends on the value you are trying to determine if the first coefficient is less than std::numeric_limits<int>::max() , then this does not apply. Let's call these prime numbers BIGPRIME ( 600851475149 would be a good example).
long long int has a size of at least 64 bits. int unlikely to be more than 32 bits on most platforms, so when it is no longer on your platform, it can rise to std::numeric_limits<int>::max() , which (again, assuming a common 32-bit platform here) 2147483647 , which in turn advances in comparison with long long int , but retains its value, which is always less than BIGPRIME . Always increasing i never succeeds, and as soon as you are on max() , you enter the UB ground, since signed integers are not ported to C ++. Your code may contain an infinite loop or do some things like writing -1 as a real factor or making you pregnant.
You can easily notice that by adding some
if( 0 == (i%100000000)){ std::cout << i << std::endl; }
into the for loop.
source share