arithmoi implements integerRoot
by getting an initial approximate root and refining its guess with Newton’s method. For (1032)786, the second approximation gets a really good starting point:
> appKthRoot 786 ((10^32)^786)
100000000000000005366162204393472
For (1032)787, the second approximation gets a really bad starting point. Like, really bad.
> appKthRoot 787 ((10^32)^787)
1797693134862315907729305190789024733617976978942306572734300811577326758055009
6313270847732240753602112011387987139335765878976881441662249284743063947412437
7767893424865485276302219601246094119453082952085005768838150682342462881473913
110540827237163350510684586298239947245938479716304835356329624224137216
It actually gets this approximation for everything starting there.
> length $ nub [appKthRoot x ((10^32)^x) | x <- [787..1000]]
1
Anyway, putting in the important parts of appKthRoot
, we get:
> let h = 106; k = 786; n = (10^32)^k; !(I# s) = h * k - k in floor (scaleFloat (h - 1) (fromInteger (n `shiftRInteger` s) ** (1/fromIntegral k) :: Double))
100000000000000005366162204393472
> let h = 106; k = 787; n = (10^32)^k; !(I# s) = h * k - k in floor (scaleFloat (h - 1) (fromInteger (n `shiftRInteger` s) ** (1/fromIntegral k) :: Double))
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
and taking a look at what’s going into scaleFloat
:
> let h = 106; k = 786; n = (10^32)^k; !(I# s) = h * k - k in fromInteger (n `shiftRInteger` s) ** (1/fromIntegral k) :: Double
2.465190328815662
> let h = 106; k = 787; n = (10^32)^k; !(I# s) = h * k - k in fromInteger (n `shiftRInteger` s) ** (1/fromIntegral k) :: Double
Infinity
Yeah, that’d do it. (1032)786 ÷ 282530 ≈ 21023.1 fits in a double, but (1032)787 ÷ 282635 ≈ 21024.4 does not.