Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Range() maximum size; dynamic or static?

I'm quite new to python, so I'm doing my usual of going through Project Euler to work out the logical kinks in my head.

Basically, I need the largest list size possible, ie range(1,n), without overflowing.

Any ideas?

like image 498
Bolster Avatar asked Jul 14 '10 15:07

Bolster


1 Answers

Look at get_len_of_range and get_len_of_range_longs in the builtin module source

Summary: You'll get an OverflowError if the list has more elements than can be fit into a signed long. On 32bit Python that's 2**31 - 1, and on 64 bit Python that's 2**63 - 1. Of course, you will get a MemoryError even for values just under that.

like image 133
Devin Jeanpierre Avatar answered Sep 29 '22 08:09

Devin Jeanpierre