2

I am using Python 3 with Google Colaboratory or the Coursera notebooks platform. I presume the problem would be the same with a local installation.

import numpy as np
def sum_numpy(N):
    return np.sum(np.arange(N)**2)

def sum_python(N):
    return sum([i**2 for i in range(N)])

sum_numpy(10**8) outputs 662921401752298880, and sum_python(10**8) outputs 333333328333333350000000

These functions give the same results with smaller numbers. For example, they both output 332833500 with 10**3.

Why don't they output the same result with 10**8 ? In theory, integers have no maximum in Python3.

PositronBeam
  • 136
  • 1
  • 6
  • 4
    Numpy integers have a limited size (`int64`), while Python integers can be long. Look at `np.iinfo('int64')` – hpaulj May 09 '18 at 07:03
  • 1
    https://stackoverflow.com/questions/13795758/what-is-sys-maxint-in-python-3 – hpaulj May 09 '18 at 07:07

0 Answers0