2

I want to compute the cube root of an extremely huge number in Python3.

I've tried the function below, as well the Python syntax x ** (1 / n), but they both yield an error:

OverflowError: (34, 'Numerical result out of range')

I really need to compute the cube-root to solve a problem in cryptography.

Binary search:

def find_invpow(x,n):
    """Finds the integer component of the n'th root of x,
    an integer such that y ** n <= x < (y + 1) ** n.
    """
    high = 1
    while high ** n < x:
        high *= 2
    low = high/2
    while low < high:
        mid = (low + high) // 2
        if low < mid and mid**n < x:
            low = mid
        elif high > mid and mid**n > x:
            high = mid
        else:
            return mid
    return mid + 1

Number:

2402723012420288965061445118057886492024743551154629917030119156891142918839022160395719857240175320389341288681749366145145967978475131123226312522685251430708710073129238467242975159749008032321188589120967227479657671719747542846097161210054623906120260301055001127882387713498723567963440422423914882959001801987608169316360033781939732394099020551125663699345546906894669213059766588861651631700807482565194653978237220624343007162119668966610411816483339553933008437525708171932730677159310906443685072653956857962468259193402008679184817845439554701795917678112318967148892417504752971110662951416459507065969183113675073168805647217161265168559090283169303349989764721815853152625319121281629196477157281162821124326395105731219640206524664156427517206350452758237590539110470955306030593745489038645874942237826327101675102728158117033520522948576476067569350309419081936561261029262542325027092539027418272029737267
Shuzheng
  • 11,288
  • 20
  • 88
  • 186
  • There are some questions about integer cube roots dotted around SO. You might find [this answer](https://stackoverflow.com/a/35276426/270986) useful. Or you might not. (Disclaimer: I wrote it.) – Mark Dickinson Sep 13 '18 at 12:08
  • You might also want to replace `high/2` with `high//2`, so that you're using integer arithmetic throughout. – Mark Dickinson Sep 13 '18 at 12:09
  • Possible duplicate of [How to find integer nth roots?](https://stackoverflow.com/questions/15978781/how-to-find-integer-nth-roots) – Mark Dickinson Sep 13 '18 at 12:27

1 Answers1

6

You can use the decimal module for precision arithmetic.

>>> import decimal
>>> decimal.getcontext().prec = 2000
>>> d = decimal.Decimal('2402723012420288965061445118057886492024743551154629917030119156891142918839022160395719857240175320389341288681749366145145967978475131123226312522685251430708710073129238467242975159749008032321188589120967227479657671719747542846097161210054623906120260301055001127882387713498723567963440422423914882959001801987608169316360033781939732394099020551125663699345546906894669213059766588861651631700807482565194653978237220624343007162119668966610411816483339553933008437525708171932730677159310906443685072653956857962468259193402008679184817845439554701795917678112318967148892417504752971110662951416459507065969183113675073168805647217161265168559090283169303349989764721815853152625319121281629196477157281162821124326395105731219640206524664156427517206350452758237590539110470955306030593745489038645874942237826327101675102728158117033520522948576476067569350309419081936561261029262542325027092539027418272029737267')
>>> d ** (decimal.Decimal('1') / 3)
Decimal('133937206273872566760548414794736210336251960742418234450742812760868006627899817648959176121676974720839395260046130578695420021242440887741838024586467483284464941105736948057343627501344322408674008360866066160169709440273267336889049052326764318741858819810142437790459410998656086770245548367092374993974.01957800067412107274671320231288433266062350364546769683592253847594623743451779703720694951107454806529528359690294725148124675778043388158795844479553929259504272303859032046859069584110837095735376305672666156318976480517890226479400274370058459434480767128207432199986761444453695779436118862839501324178398087487742940776584102213316419935313873461925911434527386116722957013094796698898872472700324272368147904802863073057018980648551662804801948996721951492839701312729619407794739229894684430062455794449661633869900331053808543005468354619833262979285703791169874072843731978696381444765081323774746768193649889006915101567415498545955639225416247411287009325988294375297958958601667209211716173048048178449049781583788393891485406794668135080292600073761513088637604929682501697901327215780654875172344481652157024703496367724775966846680098078095988613887200458438313630440073673743142814909807062482391728722162209803312904154526529349544269684966919099961646023493569723904047113508330178060117569264305364572356129475509955777812197908616643825623456854418991845353487470790610985118498418396536599578496257211629211857345844725689427541555645325863534277624999230070420282238046581410342066834795210037742864107372403222187187464238882876162544637545389362899169872326464853326158348354630857803436152663716863966593687094693903986337682985705052025525327446033300460743420392444819670886274886235041470468968990640496098175190257430642679726676520803445140667726438592299927612919060753366359346573915491216624232420766373849803692518435531717789932103622716420370476975692356853583654180285339707501578137893768162870141488833638142460328356566642144778402818983167642943155')
AKX
  • 152,115
  • 15
  • 115
  • 172