Console outputs/prints nothing.

Mar 1, 2016 at 5:54pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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;
			}
	}
}
Last edited on Mar 1, 2016 at 6:06pm
Mar 1, 2016 at 6:04pm
http://www.cplusplus.com/forum/beginner/1988/

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.
Mar 1, 2016 at 6:18pm
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.
Mar 1, 2016 at 6:22pm
But it prints nothing even when I use long long, and it doesn't display any warning or error.
Mar 1, 2016 at 6:32pm
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:
long long int 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 
    }
Last edited on Mar 1, 2016 at 6:40pm
Topic archived. No new replies allowed.