python int is infinite

Did you know! There is no limit to the number of python ints that can be represented! !! Since int in C is 32bit, it can only represent up to 2147483647. Even the long long used when dealing with large numbers can only represent 64 bits, that is, 9223372036854775807. On the other hand, ints in python are infinite.

Well then

When I hear that there is no upper limit, it is humanity that makes me want to handle huge numbers of feces. To see if there is really a limit, and if there is no limit, how long the calculation time is I did a simple experiment! Actually check the time required for calculation and the time required for display based on the following code.

test.py


num_list, time_list = [], []
for i in range(9):
    start = time.time()
    ans = 10 ** (10 ** i)
    end = time.time()
    print('Elapsed time to calculate 10 ** {} is {}'.format(10 ** i, end - start))
    num_list.append(10 ** i)
    time_list.append(end - start)
plt.plot(num_list, time_list)
plt.show()

The experimental results are shown in the table below! It's hard to understand, but the numbers on the far left of the table represent the $ n $ part of $ 10 ^ n $. Therefore, 100 in the top column of the table is the time it took to calculate and display $ 10 ^ {100} $.

10^n calculate[s] print[s]
100 0.000 0.000
1000 0.000 0.000
10000 0.000 0.002992
1e05 0.01562 0.1396
1e06 0.1562 13.27
1e07 5.985 1324
1e08 221.7 -
1e09 8791 -

Even such a large number can be calculated properly, but it takes a lot of time. When $ n $ increases 10 times, the calculation time will increase 40 times and the display time will increase 100 times. By the way, even if you say "display", the value is too large to display it on the console due to a bug (naturally). Also, print 1e08 and 1e09 are No data because it took too long and I got tired of it. The figure that the bug is displayed from 0 →![Print_bag.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/419937/c813ae25-9631-1129- 6c24-2b0c1afd528e.png)

The graph is below. As I mentioned above, it looks like it will increase exponentially! calculate_time.png print_time.png

At first I started thinking about calculating Googolplex, but in the first place     log2(10^{10^{100}}) \simeq 3.32*10^{100}bit = 4.15*10^{87}TB It was a net impossible story because it only requires memory!

Result: A huge number can be represented!



What did you think? I tried to write an article in a Wazap-like tone, What's the Wazap-like tone? I'm not sure because it has become. Supplement: A microcosm of a modern society where only one person running the program does his best ![resource.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/419937/29e47660-2cbf-bb96-08af-5fb51017c6a3.png)

Recommended Posts

python int is infinite
Python is easy
What is python
Python is instance
What is Python
[Python] What is Pipeline ...
[Python] What is virtualenv
Python round is not strictly round
[Python] Debugging is more efficient!
Convert numpy int64 to python int
How Python __dict__ is used
Python is painful. But use
Python is an adult language
Python list is not a list
Python immutable type int memo
[Python] Python and security-① What is Python?
Python release cycle is faster!
[Python] * args ** What is kwrgs?
Identity and equivalence Python is and ==
What is a python map?
Python Basic Course (1 What is Python)
What is Python? What is it used for?
[Python] What is a zip function?
[Python] What is a with statement?
Python Note: About comparison using is
ppa: jonathonf / python-3.6 is now private
Difference between == and is in python
Python
Use fabric as is in python (fabric3)
Golang vs. Python – Is Golang Better Than Python?
Python is UnicodeEncodeError in CodeBox docker
Infinite prime number generator in Python3
[Python] What is @? (About the decorator)
Which is better, PyPy or Python?
[python] What is the sorted key?
Python for statement ~ What is iterable ~
There is no switch in python
Today's python error: image is blank
Infinite product in Python (using functools)
What is the python underscore (_) for?
Determining which OS is running Python
Python> What is an extended slice?
Python in is also an operator
[Python] What is pandas Series and DataFrame?
python note: when easy_install is not available
[Python] Name Error: name'urlparse' is not defined
[Python] What is inherited by multiple inheritance?
What is NaN? NaN Zoya (Python) (394 days late)
"<" In python> pack ("<L", ...) is little endian
Import error even though python is installed
[Python] [pandas] How is pd.DataFrame (). T implemented?
Inject is recommended for DDD in Python
What kind of programming language is Python?
Python learning basics ~ What is type conversion? ~
Python memo using perl --format is convenient.
Why Python is chosen for machine learning
Where is the python instantiation process written?
What is "mahjong" in the Python library? ??
Hash in Perl is a dictionary in Python
What is a dog? Python installation volume
[python] [meta] Is the type of python a type?