Home > OS >  Power operation with large integers in Python
Power operation with large integers in Python

Time:07-12

Python natively supports very large integers. The math library's isqrt computes the integer square root. Is there a function that raises an integer to an arbitrary power? The pow function converts to float and overflows for large results:

math.floor(math.pow(2**200, 1/3))    # No error
math.floor(math.pow(2**20000, 1/3))  # OverflowError: int too large to convert to float

I am looking for something like an ipow function that does not overflow.

CodePudding user response:

The decimal module can handle this, for example:

import decimal
ctx = decimal.getcontext()
ctx.prec = 6500
exponent = ctx.divide(20000, 3)
result = ctx.power(2, exponent)
print(int(result))

Output is

735593291484387209783675421417401551388528910419937498855489316037238729877799194785967117680650787243499578936739367211646336702910364372751490577134289059381103150173449511169132581463262818907490475409551772062595046554619384379764111283816833130114931044558055461040744776094933108099995352859249587308681118468618551737491233589578046534330578500833584313798043658599325166896940440026560000674155494425561529093093385227568115767603311546934753065615354651179034856424947492559998314813723232126047986199186692924322209453842523270144564254918521895891894138942324165870270918720979955876864460887030503591128466057960095921059421570370683311998361243854102019912476812346628152873679099875731992812581372090476077840642766417162231457141312398378060032090686413445173632160421276366980693101437915303356336005141806380919125651101412427041852060401414834166014760367877496395583175233906237654399585708415664501788202720166540417353255100517212193430918948683006053690334203911185126327872999968095816493502031192518787364946015189113760776466681987952552649552364498065143893821479865926801298189255763765916482517091364704275308800001539819875298342279690446978366086810899478379982765040873975435085712668084499130012812114825234734299814355689769620299939348424592076693093628320682250491216036069108332682422958615277131491671823381808612038168371599560137528553793535487108057516732518398596025223142626252566110145697992447977257943643009508373299549949314341651520552962657771425814947511356257661097080935349597402847684879273016276252463198399172337268560967426151100341786305896623994943237562840978866280921329036015617665889760004248213234466321167050001673745181387180054105039594410860856665966905963214479595518747888234899510061024971651516692404399410754298416762001487679586513213265170946091382441269359664469661092905377348325500898481376014130308088128596765169247928523519626652934624525059980867163038359798545916814792106534516848172601470415547938750024224289813196267713583

CodePudding user response:

mpmath is a free (BSD licensed) Python library for real and complex floating-point arithmetic with arbitrary precision

import mpmath as mp
>>> mp.power(mp.power(2,20000),1/3)
mpf('7.3559329148419845e 2006')
  • Related