Answer to Question #260174 in Python for nani

Question #260174

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


1
Expert's answer
2021-11-02T17:35:45-0400
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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog