Question #260174

given an integer N the task to find the minimum absoulte difference between N and a closest N number power of 2


Expert's answer

from math import floor,log2
def AbsoluteDifference(num) :
    N1 = pow(2, floor(log2(num)))
    N2 = N1 * 2
    return min((num - N1), (N2 - num))


num = int(input("Enter the interger n: "))
print("Mean absolute difference is: ",AbsoluteDifference(num))

LATEST TUTORIALS
APPROVED BY CLIENTS