請教一個Python中長整數的問題

夢飛發表於2017-04-22

以下是求兩個向量和的簡單程式,但是當用NumPy包時卻得到了錯誤的答案。為什麼?

enter image description here

import numpy as np 
def numpysum(n):
  a = np.arange(n) ** 2 
  b = np.arange(n) ** 3 
  c = a + b

  return c

def pythonsum(n): 
  a = range(n) 
  b = range(n) 
  c = []

  for i in range(len(a)): 
      a[i] = i ** 2 
      b[i] = i ** 3 
      c.append(a[i] + b[i])

  return c

size = 2000

c = pythonsum(size)
print "The last 2 elements of the sum", c[-2:]
c = numpysum(size)
print "The last 2 elements of the sum", c[-2:]

相關文章