range and xrange for 13-digit numbers in Python?

2019-01-15 07:24发布

问题:

range() and xrange() work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.

回答1:

You could try this. Same semantics as range:

import operator
def lrange(num1, num2 = None, step = 1):
    op = operator.__lt__

    if num2 is None:
        num1, num2 = 0, num1
    if num2 < num1:
        if step > 0:
            num1 = num2
        op = operator.__gt__
    elif step < 0:
        num1 = num2

    while op(num1, num2):
        yield num1
        num1 += step

>>> list(lrange(138264128374162347812634134, 138264128374162347812634140))
[138264128374162347812634134L, 138264128374162347812634135L, 138264128374162347812634136L, 138264128374162347812634137L, 138264128374162347812634138L, 138264128374162347812634139L]

Another solution would be using itertools.islice, as suggested inxrange's documentation



回答2:

No problems with creating the range, as long as you don't want 10**13 elements, e.g.

range(10**14,10**15,10**14)

gives

[100000000000000, 200000000000000, 300000000000000, 400000000000000, 500000000000000, 600000000000000, 700000000000000, 800000000000000, 900000000000000]


回答3:

if you need enumerating integer try using itertools:

itertools.count(1000000000000)

it should not allocate memory for a list of 1000000000000 elements



回答4:

I don't think it will work. Functions like len expect the result to fit into a 4 byte integer, due to restrictions in the cPython implementation.

In Python 3.0:

>>> range(9999999999999)
range(0, 9999999999999)

It looks like it works, but...

>>> len(range(9999999999999))
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    len(range(9999999999999))
OverflowError: Python int too large to convert to C ssize_t

See here for a related question.



回答5:

On 64-bit Python:

>>> xrange(9999999999999)
xrange(9999999999999)

I would not use range() for a 13-digit number. My poor machine would not be able to hold the resultant list.



回答6:

range(x) returns a list.Python lists cant contain that many elements. You should use xrange() to iterate through those digits if you need to do trillions of cycles.?



回答7:

range() and xrange() work in recent enough versions of Python; however, in 2.5 or less you'll need to work around the int to long conversion.

def irange(start, stop=None, step=1):
    if stop is None:
        stop = long(start)
        num = 1L
    else:
        stop = long(stop)
        num = long(start)
    step = long(step)
    while num < stop:
        yield num
        num += step

This isn't a complete solution (it doesn't handle negative steps), but it should get you going.



回答8:

The difference between range() and xrange() is that the first returns the entire list, while the second returns a generator that generates each number as it is needed. The second one should work for any number, no matter how large.

In Python 3.0, xrange() has disappeared and range() behaves like xrange() did previously.



回答9:

For sollution of this problem you don't need such long numbers, because you need only prime factors, you can use square root:

for i in xrange(2, int((n+1)**0.5)):