Today I wrote a program using an array/list with 64000000 entries. However, when writing sigma=[1]*64000000 using Python it runs fine, but later on, as the program computes, my Ubuntu freezes - without any reaction to inputs, not even mouse movement. I tried twice and the results are the same.
When implemented in C++, long long sigma[64000000] holds up fine and runs very fast.
Is there any reason that my program would freeze in the middle of running, other than crashes at beginning?
EDIT: To reply to Chris below, my code did not freeze until a couple of loops later.
Thank you all!
For those who interested in seeing the code, this is the program, a brute-force Project Euler 211:
def e211():
ans=0
sigma=[1]*64000000
for i in range(2,64000000):
j=i;
if ((j%1000==0) or (j<100)):
print(j)
q=i*i
while j<64000000:
sigma[j]+=q
j+=i
for i in range(1,64000000):
j=int(sqrt(sigma[i]))
if j*j==sigma[i]:
ans+=i
if __name__=='__main__':
print(e211())
-
You allocated a 500MB array on the stack in C++?Nicholas Knight– Nicholas Knight2011年05月30日 04:52:47 +00:00Commented May 30, 2011 at 4:52
-
Which version of python are you using ?Pavan Yalamanchili– Pavan Yalamanchili2011年05月30日 04:54:14 +00:00Commented May 30, 2011 at 4:54
-
@Nicholas Well that's daily meal for contest programmers:)zw324– zw3242011年05月30日 04:56:15 +00:00Commented May 30, 2011 at 4:56
-
@Ziyao Wei, on a side note, How many have you solved so far ?Pavan Yalamanchili– Pavan Yalamanchili2011年05月30日 05:07:58 +00:00Commented May 30, 2011 at 5:07
-
@Pavan Up until now, 164. Working towards the next level:) How about you?zw324– zw3242011年05月30日 05:28:33 +00:00Commented May 30, 2011 at 5:28
2 Answers 2
Python lists are lists of objects. A number in Python is an object in itself, and takes up somewhat more storage than the 64 bits needed to represent a long long in C++. In particular, Python transparently handles numbers larger than 32 bits, which end up taking a lot more space than a simple integer.
You may be find the standard Python array module useful. It provides Python-compatible access to uniform arrays of integers of specified size. (However, I note that it doesn't offer a 64-bit integer type.)
2 Comments
range(1,64000000):
This line creates a full list of size 64000000, so now you have two lists of that size in memory. Use xrange instead.