Numbers and Python

It has occurred to me two times now where I wanted to be able to make some mathematical calculations that required extra digits or precision that the programming language Python was able to do it. It is a common language and is automatically installed in Linux, which is my desktop of choice.

Here are some examples that show the use of Python to calculate very large numbers, calculate the value of large numbers to a modulus, and divide two numbers with a high precision.

[jim@intrexcore2 ~]$ python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:38:36)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 9*2
18
>>> 112**56
5704390783543055148497052427105142545007094319455466186753181001768062370545397028706212973044775021043640818466816L
>>> 22**789
1484209368987055664835267513269020782056572913665389360710993235647353654600718051214561226506395746917004454295231135392233954113209970324078422930829669659889790905015109099651816468416993153345796393249082190880560779799148367444271413281501278491572945389083004439689952805484461387751484188365768374674576495326864496558951018029848269444776287140869585391950132327105186456206323012792043081538208599784776172953593340354592284195309329894679943469851515548384450045890081875258294527235570832847624567513125295640967560191880440775646987692835066327960934486610784531403318281705562313166764297138553633645886465005542649427406806999953498677068116542246603770298508504920168233638748979619525844005160145942935955584019757060864581833018122836439007581000295288803969945931431917589136944077264684283303334162875035591187475906443149001756520978726471815621610519317668085293630518278640986694188826533120948387000017232262211213251551614215463765292579592160977582712236672133383215635362453885390795701099214030569950513308956961106465196847233236992L
>>> 22**789%532
316L
>>> from decimal import *
>>> getcontext().prec = 100
>>> 50.0/58
0.86206896551724133
>>> Decimal(50) / Decimal(58)
Decimal('0.8620689655172413793103448275862068965517241379310344827586206896551724137931034482758620689655172414')
>>>

 

 

Here I calculated 112 to the 56th power, 22 to the 789th power, and 22 to the 789th power modulus 532. Next I used the decimal package to calculate the value of 50 divided by 58. The normal result shows about 20 decimal places. However, with the decimal package and setting the desired precision to 100 we can see that this fraction takes about 29 decimal places to start to repeat.

Lastly, I should add that all of these results returned instantly.