Log precision in python

Below is the source code that checks if this number can be expressed, but why the code does not work for n = 76 ** 89 - 1and n = 76 ** 89. How can I solve this error? For both n givesx=log(n,2)/log(i,2)=89.0

from math import log,sqrt,floor
import sys
n= 76 ** 89 - 1
t=floor(sqrt(n))+1
flag=False


for i in range(2,t):
    x=log(n,2)/log(i,2)
    print(x)
    if x-int(x)<sys.float_info.epsilon:
        print("YESSSSSSSSSSSSS!")
        flag=True
        break

if not flag:
    print("Nooooooooooooooooooo!")
+4
source share
1 answer

Your code only finds candidates, but does not check if they really match. A floating point inaccuracy makes it impossible to make the difference between a very large value like this and the same value minus one.

But since python has a built-in unlimited range of integer artifacts, you can check that what you find is really consistent.

: , ( ), integer .

from math import log,sqrt,floor
import sys
n = 76 ** 89
t=floor(sqrt(n))+1
flag=False


for i in range(2,t):
    x=log(n,i)  # faster than x=log(n,2)/log(i,2)

    if x-int(x)<sys.float_info.epsilon:
        x = int(round(x))
        r = int(round(n**(1/x)))
        print("found candidate: ",x,r)
        if n == r**x:   # exact integer comparison with initial value & found values
            print("YESSSSSSSSSSSSS!")
            flag=True
            break
        else:
            print("but not exact")

if not flag:
    print("Nooooooooooooooooooo!")

76 ** 89 - 1, " ", n.

, x=log(n,2)/log(i,2) x=log(n,2)/log(i,2) x=log(n,i) , , , .

+2

Source: https://habr.com/ru/post/1691564/


All Articles