range(1, 600851475143)wants to create a very large list in memory, and you get an error in memory. To save memory, use xrangeinstead range. Unfortunately, it xrangedoes not work with large numbers (this is an implementation restriction) Example (throws an OverflowError):
for i in xrange(1, 600851475143):
print i
You can have large minimum or maximum values ββin your interval with rangeif their difference is small. Example:
x = 1 << 200
print list(xrange(x, x + 3))
Output:
[1606938044258990275541962092341162602522202993782792835301376L, 1606938044258990275541962092341162602522202993782792835301377L, 1606938044258990275541962092341162602522202993782792835301378L]
Unusual solution to the problem with your original for the loop:
def bigrange(a, b = None):
if b is None:
b = a
a = 0
while a < b:
yield a
a += 1
for i in bigrange(1, 600851475143):
print i
, , continue:
i = 1 - 1
while i < 600851475143 - 1:
i += 1
print i