1

I am trying to take cube root of a big number in python.

m = math.ceil(x ** (1 / 3))

And it throws the following error. I'm new to python Any idea how I can fix this? This code works well with small numbers.

OverflowError: int too large to convert to float

x: 181585216044493265110479777246620298734422341767351687340163849056661430780245822370036270576390671679383160478373098420103368845901477954418397846307160299618708880188789598804503740782461426628087734922371474590103627994348980310908410238370700959464384104051608095487808582735974143598352840162507220364226700149908540696739595513449394546491358752802464480369408135010751665163873817561907562326413519526842502450873431095146206977010971723964333483444586528175829418205819889122383336398168745171506616857763743219777019746656312

Edit: x is the result that I got from Chinese Remainder Theorem, and I need all integer ceil digits for m. Note: I cannot use any other imports.

Anu
  • 31
  • 7
  • 1
    Check [this](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex) – S-Flavius Mar 18 '22 at 09:20
  • How many digits of the result do you want ? Where is this number coming from ? –  Mar 18 '22 at 09:21
  • Updated my question. x is the result that I got from Chinese Remainder Theorem, and I need all integer ceil digits for m. – Anu Mar 18 '22 at 09:54
  • @Anu I just programmed a [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm)-based solution for computing precise value of your cube root for given example of your number, [here is code](https://cutt.ly/5SfH8HS), please put a look. If your question wasn't closed I would post this code as my Answer. – Arty Mar 18 '22 at 12:12
  • 1
    @Anu If you read my previous comment with code, then I decided to implement a second method, which uses [Newton's Method](https://en.wikipedia.org/wiki/Newton%27s_method) of finding roots (zeros) of a function. It should be faster than binary search in general. [Code is here](https://cutt.ly/USf0IPB), put a look. – Arty Mar 18 '22 at 12:43
  • @Arty I used the Newton's method and it worked. Finally! Thank you soooo much. – Anu Mar 18 '22 at 13:34
  • @Anu See that I posted two different comments above. First is Binary Search method, second is Newton's Method. Both methods are very fast and both work and give same cubic root. Please put a look at both of them. If this Question was some University task then both methods will be good to show University teacher that you have knowledge of more than one methods of solving your task. – Arty Mar 18 '22 at 16:05
  • 1
    @Anu Out of my two solutions above I created [this answer](https://stackoverflow.com/a/71530812/941531), please put a look at it to see both algorithms working together with timings. – Arty Mar 18 '22 at 17:16

0 Answers0