Sean Morgan Sean Morgan - 14 days ago 9
Python Question

Why does Python say "Inappropriate Argument Type" for xrange?

My code is:

def euler7():
prime=1
val=0
for num in xrange(3, 9999999999, 2):
for n in xrange(1, num):
if num%n==0:
numb=0
break
else:
numb=1
val=num
if numb==1:
prime=prime+1
if prime==10001:
return val


But, it says there is an Inappropriate argument type for the for loops. I dont know if that means that it is not seeing the values as integers, or what is happening. Thanks

Answer

xrange won't work with large numbers since the code is executed as a C program (https://docs.python.org/2/library/functions.html#xrange).

According to docs -

CPython implementation detail: xrange() is intended to be simple and fast. Implementations may impose restrictions to achieve this. The C implementation of Python restricts all arguments to native C longs (“short” Python integers), and also requires that the number of elements fit in a native C long. If a larger range is needed, an alternate version can be crafted using the itertools module: islice(count(start, step), (stop-start+step-1+2*(step<0))//step).

I am not sure what system specs you got, but generally c int is 2^16-1 and c long is 2^32-1.