-1

I have this for-loop:

for i in range(1000000000, 1000000030):
    foo(i)

When I execute it this error is given:

Traceback (most recent call last):
  File "/CENSORED/Activity.py", line 11, in <module>
    for i in range(1000000000, 10000000030):
OverflowError: range() result has too many items.

As far as I know, this range-object should have exactly 30 elements...

Where is the problem?

Edit:

I have removed the extra zero, now I get this:

Traceback (most recent call last):
  File "/CENSORED/Activity.py", line 12, in <module>
    factorizeInefficient(i)
MemoryError

Edit 2:

def factorizeInefficient(n):
    teiler = list()
    for i in range(n):
        if i != 0:
            if (n%i)==0:
                teiler.append(i)
    print teiler

Just found the solution myself: There is a range(n) object in this as well and this causes the memory Error...

An extra question: How did you guys know this was python 2? (Btw you were right...)

monamona
  • 1,195
  • 2
  • 17
  • 43
  • Take a look [here](https://stackoverflow.com/questions/10778764/python-overflowerror#10778815), you might find it useful. – Vasilis G. Nov 28 '17 at 19:06
  • If you're on python 2.7, then you should use xrange instead of range. – Jakob Lovern Nov 28 '17 at 19:07
  • 1
    Because you are on Python 2, and `range` will create a `list` object. This list-object will require, *just for the pointers in the underlying array*, something like `9000000030 * 8 * 1e-9 == 72` gigabytes. You can add at least another 28 bytes per actual `int` object contained in the list, bringing you to `297` gigabytes – juanpa.arrivillaga Nov 28 '17 at 19:08
  • @VasilisG. But this range object should only generate the 30 numbers inbetween my numbers, shouldn't it? – monamona Nov 28 '17 at 19:09
  • Range is creating a list that is too big and it is running out of memory. – NendoTaka Nov 28 '17 at 19:09
  • @monamona there aren't 30 ints. there are `10000000030 - 1000000000 == 9000000030` ints. – juanpa.arrivillaga Nov 28 '17 at 19:09
  • 1
    You have an extra 0 in the second number – NendoTaka Nov 28 '17 at 19:09
  • @NendoTaka I removed it, then I got an memoryError instead ^^ – monamona Nov 28 '17 at 19:10
  • @monamona I think that @juanpa.arrivilaga is right. You must have enter an extra `0` in `range`'s end value. – Vasilis G. Nov 28 '17 at 19:11
  • 1
    `range()` in Python 3 is like `xrange()` in Python 2: it returns an iterator in Python 3 instead of a list. So Python 3 doesn't care how many elements it has. Python 2 has to construct a list with that many elements, and it was exceedingly unlikely you're running on a machine with enough memory to hold 9 billion ints ;-) – Tim Peters Nov 28 '17 at 19:13

2 Answers2

1

count your zeros once again ;) I'd say it's one too much.

Sim Son
  • 310
  • 1
  • 10
1

Copy/pasting the range() part of your code:

>>> len(range(1000000000, 10000000030))
9000000030

So there are actually about 9 billion elements in the range. Your first argument is presumably missing a zero, or second argument has a zero too many ;-)

Tim Peters
  • 67,464
  • 13
  • 126
  • 132
  • 1
    Note that these errors are more easily avoided if you write large numbers in arithmetic, i.e.: `range(1000 * 1000 * 1000, 1000 * 1000 * 1000 + 30)` It saves you time keeping track and your computer won't even notice the extra work. – Mattias Martens Nov 30 '17 at 19:06