#include "stdafx.h"
#include <iostream>
int main(){
int n;
int m;
for (n = 1; n <= 600851475143; n++);
while (m = 1, m <= 600851475143){
m++;
if (m%n != 0){
std::cout << m;
}
}
}
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
600851475143 requires at least 40 bits to represent. Usually a signed int is limited to a range of 31 bits.
Because of that, when I compile the above code I get these messages:
[Warning] comparison is always true due to limited range of data type [-Wtype-limits]
[Warning] comparison is always true due to limited range of data type [-Wtype-limits]
for lines 8 and 9.
This means the program will enter an infinite loop at line 8.
When you use long long you may just have to be very patient, waiting for the empty loop at line 8 to complete. It would be more efficient to simply put: longlongint n = 600851475143 + 1;
rather than waiting a long time for the same result.
The second loop is also an infinite loop.
Each time around, m is re-initialised to 1, tested to see if it is within range, then the body of the loop is executed, m is incremented to 2. Then the whole process repeats.
edit: note the first loop ends with a semicolon, like this:
for (n = 1; n <= 600851475143; n++);
The semicolon represents an empty statement. In effect the same as this:
1 2 3 4
for (n = 1; n <= 600851475143; n++)
{
// nothing here
}