Marcin Zdunek - 5 months ago 8

Python Question

While trying to run this code:

`l = 1000000`

w = [1, 1]

for i in range(2, l):

w.append(w[-1] + w[-2])

computer hangs on and Blue screen of death appears. The only info which I get is about MEMORY MANAGEMENT. Problem occurs in version 2.7 of Python and 3.4 as well.

Code works good for

`l = 100000`

Can someone explain me exactly why? I am using Windows 10 64-bit, Python 2.7.8 64-bit from Active Python.

EDIT:

Here is R code which works well:

`len <- 1000000`

fibvals <- numeric(len)

fibvals[1] <- 1

fibvals[2] <- 1

for (i in 3:len) {

fibvals[i] <- fibvals[i-1]+fibvals[i-2]

}

Answer

The numbers you're producing are huger than you might realize. For example, here's the size in memory of the last one:

```
>>> a, b = 1, 1
>>> for i in xrange(2, 1000000):
... a, b = b, a+b
...
>>> sys.getsizeof(b)
92592
```

That's 92 kilobytes for one integer. All of them put together would be somewhere in the vicinity of 46-ish gigabytes, and you only have 16 gigabytes.

Your R code used 64-bit floating-point numbers, which promptly overflow to infinity at around the 1476th number.

Source (Stackoverflow)

Comments