given an integer N the task to find the minimum absoulte difference between N and a closest N number power of 2
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))
Comments
Leave a comment