Floor of Natural Logarithm

1 week ago 6
ARTICLE AD BOX

I have an arbitrarily large integer n. How would I calculate the exact value of ⌊ln(n)⌋?

int(math.log(n)) fails, as math.log(n) might not be accurate enough (e.g. if given 4311231547115195 it outputs 36 but should output 35). I know that ⌊ln(n)⌋ is either ⌊n.bit_length()-1ln(2)⌋ or ⌊n.bit_length()ln(2)⌋. However, in addition to this having the same problem of floating point precision, if the two are unequal I don't know which one to choose.

I've also tried computing lower and upper bounds on the value of n.bit_length()ln(2). I tried just using rational approximations of ln(2) given by the harmonic series. However, the alternating harmonic series converges very slowly, and it takes a very long time in some cases for the values to come close enough for me to determine ⌊n.bit_length()ln(2)⌋ and ⌊n.bit_length()-1ln(2)⌋, and I still don't know what to do if they're unequal.

I would also appreciate solutions for ⌊ln(n)2⌋, ⌊ln(n)3⌋ and ⌊ln(n)4⌋.

Read Entire Article