Storing a very large number

Gary Herron gherron at digipen.edu
Wed Jul 17 16:02:25 EDT 2013


On 07/17/2013 12:21 PM, Hasit Mistry wrote:
> I came across a problem that requires me to store a very large number 
> (say >10^100). How do I do it efficiently?
> And also, how do I select a particular number (say 209th) from that 
> very large number?
> I am relatively new to Python.
>
> Thank you in advance.
>

Python already has long numbers (integers) built in.:

 >>> 10**100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L

The 'L' on the end notifies you it's a *long* int.  I can't speak about 
its efficiency, but I assume it's OK.

By 209th number, do you mean 209th *digit*?    I'd say just get a string 
representation and index the 209th character:

 >>> str(12**345)
'2077446682327378559843444695582704973572786912705232236931705903179519704325276892191015329301807037794598378537132233994613616420526484930777273718077112370160566492728059713895917217042738578562985773221381211423961068296308572143393854703167926779929682604844469621152130457090778409728703018428147734622401526422774317612081074841839507864189781700150115308454681772032'
 >>> str(12**345)[209]
'1'

Gary Herron


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418




More information about the Python-list mailing list