0

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

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126

2 Answers2

1

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.

hyades
  • 3,110
  • 1
  • 17
  • 36
  • The point where Python starts using `long` depends on the platform, and `sys.maxint` lets you query that point. On a 32-bit Python executable that point is at 2147483648 (10 digits). Longs are only bound by memory. – Martijn Pieters Dec 24 '14 at 09:05
  • @MartijnPieters but here I think they are still using the *C long* which will be 2^32. Python `long` wont be used in here. – hyades Dec 24 '14 at 09:20
  • I meant merely to inform about the Python 'short' here. The documentation quoted indeed talks about C longs. – Martijn Pieters Dec 24 '14 at 09:23
-1

From the docs:

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)//step).

The point where Python starts using long depends on the platform, and sys.maxint lets you query that point. On a 32-bit Python executable that point is at 2147483648 (10 digits). Longs are only bound by memory.

I will suggest to re-implement xrange() using generators:

def myxrange(a1, a2=None, step=1):
    if a2 is None:
        start, last = 0, a1
    else:
        start, last = a1, a2
    while cmp(start, last) == cmp(0, step):
        yield start
        start += step
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126