Main () does not execute, but compiles

I have this simple program:

// Include libraries #include <iostream> #include <string> #include <vector> using namespace std; // Include locals // Start #define NUMBER 600851475143 int main(int argc, const char* argv[]) { long long int ans = 0; long long int num = NUMBER; vector<int> factors; do { // Get lowest factor for (int i = 1; i <= num; ++i) { if (!(num % i)) { factors.push_back(i); num /= i; break; } } } while (num > 1); cout << "Calculated to 1.\n"; int highestFactor = numeric_limits<int>::min(); for (int i = 0; i < factors.size(); ++i) { if (factors[i] > highestFactor) { highestFactor = factors[i]; } } ans = highestFactor; cout << ans << endl; return EXIT_SUCCESS; } 

compilation with g++ -O2 -c -o prob3.o prob3.cpp was successful, but when I started it, I didn’t see anything, and it just continued to work, and in the end I had Ctrl-C (force-kill). When i try to add

 int main(int argc, const char* argv[]) { cout << "Test\n"; 

for the program, Test was not printed either. This is how my program does not execute at all.

Any help or advice is appreciated!

Decision

I forgot that prime numbers start with 2. Change for (int i = 1 to for (int i = 2 .

+6
source share
4 answers

Those nested loops will be cyclically forever. The inner for loop will only run once due to break , so it will only do num /= 1 . This means that num never decreases and therefore num > 1 will never be false. I guess you just have to wait longer!

The reason you don't see the "Test" is probably because you did not reset the result. Try:

 std::cout << "Test" << std::endl; 
+7
source

Your program is just running. It will take a lot of time.

For cout << "Test\n"; cout stream stream is not cleared: what you wrote in the stream is still in your program memory and has not yet been cleared of the system you are printing.

+2
source

Have you tried running your condition from 2? The module function does not make sense if you start with 1.

if (! (num% i))

Num / 1 gives 0, so you do not enter the if condition

+1
source

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.

0
source

All Articles