How to compute the nth root of a very big integer

后端 未结 10 643
暖寄归人
暖寄归人 2020-12-01 06:28

I need a way to compute the nth root of a long integer in Python.

I tried pow(m, 1.0/n), but it doesn\'t work:

OverflowError: lo

10条回答
  •  天涯浪人
    2020-12-01 07:02

    If you are looking for something standard, fast to write with high precision. I would use decimal and adjust the precision (getcontext().prec) to at least the length of x.

    Code (Python 3.0)

    from decimal import *
    
    x =   '11968003966030964356885611480383408833172346450467339251\
    196093144141045683463085291115677488411620264826942334897996389\
    485046262847265769280883237649461122479734279424416861834396522\
    819159219215308460065265520143082728303864638821979329804885526\
    557893649662037092457130509980883789368448042961108430809620626\
    059287437887495827369474189818588006905358793385574832590121472\
    680866521970802708379837148646191567765584039175249171110593159\
    305029014037881475265618958103073425958633163441030267478942720\
    703134493880117805010891574606323700178176718412858948243785754\
    898788359757528163558061136758276299059029113119763557411729353\
    915848889261125855717014320045292143759177464380434854573300054\
    940683350937992500211758727939459249163046465047204851616590276\
    724564411037216844005877918224201569391107769029955591465502737\
    961776799311859881060956465198859727495735498887960494256488224\
    613682478900505821893815926193600121890632'
    
    minprec = 27
    if len(x) > minprec: getcontext().prec = len(x)
    else:                getcontext().prec = minprec
    
    x = Decimal(x)
    power = Decimal(1)/Decimal(3)
    
    answer = x**power
    ranswer = answer.quantize(Decimal('1.'), rounding=ROUND_UP)
    
    diff = x - ranswer**Decimal(3)
    if diff == Decimal(0):
        print("x is the cubic number of", ranswer)
    else:
        print("x has a cubic root of ", answer)
    

    Answer

    x is the cubic number of 22873918786185635329056863961725521583023133411 451452349318109627653540670761962215971994403670045614485973722724603798 107719978813658857014190047742680490088532895666963698551709978502745901 704433723567548799463129652706705873694274209728785041817619032774248488 2965377218610139128882473918261696612098418

提交回复
热议问题