Storing a very large number

H

Hasit Mistry

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.
 
G

Grant Edwards

I came across a problem that requires me to store a very large number
(say >10^100). How do I do it efficiently?

First you must define "efficient".

If you want to store the least number of bytes, and all you need to do
is store the number (you don't need to manipulate it numerically, then
storing it as an ascii string in exponential notion might be the
smallest and fastest:

veryLargeNumber = "10^100"

That's only 27 bytes (CPython 2.7 on Linux).

If you do need to do math on it, then what's wrong with this?

veryLargeNumber = 10**100

That takes 58 bytes, but is much simpler for doing calculations.
And also, how do I select a particular number (say 209th) from that very
large number?

I don't know what you mean by "a particular number". Do you mean you
want to know the value of a particular digit when it's representing in
a partuclar base (e.g. base 10)?

Does this do what you want?

First, we'll generate a number with a lot of digits:
619.8859949077654

It has 600+ digits and requires 288 bytes to store.

Counting from the left-hand side, starting '7' as digit 0, the 209th
digit in the base-10 representation is:
'3'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top