Python large list error -
i did following program generate list of consecutive numbers. however, computations seem fail more 70.000 elements in list. tried using pycharm ide , python console. result same. i'm using python 3.4.1 32-bit version. should ? can cause ?
from pylab import * = 100000 # number of elements my_array my_array = [i in range(a)] missing_number = randint(a) print('generate random number: ', missing_number) my_array.remove(missing_number) # remove random generated number my_array print('the number of elements of list is: ', len(my_array)) #length of my_array print('the sum of list :',sum(my_array)) # sum sum02 = (a *(a-1)/2) # sum of consecutive numbers print('the complete sum of consecutive numbers:',int(sum02),'\n') print('and missing number is:', int(sum02) - sum(my_array))
i reproduce result have locally on machine :
c:\util\python34\python.exe "find_missing_number_2.py"
generate random number: 15019 number of elements of list is: 99999 sum of list : 704967685 complete sum of consecutive numbers: 4999950000 , missing number is: 4294982315 process finished exit code 0
it doesn't result error. it's doing wrong calculations can see if compare 2 variables: missing_number 1 resulted int(sum02)-sum(my_array)
from pylab import *
from numpy import *
. includes numpy.sum function explicitly says arithmetic modular when using integer types, , no error raised on overflow.
to avoid used builtin sum function, either shown reut sharabani, or not doing from pylab import *
, bad practice anyway. can replace built-in functions, without noticing. far know, replaces @ least sum , @ moment, i'm not sure that's all, , can't sure won't replace others in future.
Comments
Post a Comment