How to check if a given number is a power of two?

前端 未结 5 856
说谎
说谎 2020-12-01 16:23

The code below isn\'t working right for some inputs.

a, i = set(), 1
while i <= 10000:
    a.add(i)
    i <<=         


        
5条回答
  •  离开以前
    2020-12-01 16:38

    I have written a python function that will check the power of any number:

    import math
    def checkPowTwo(num):
      x = int(input("The power of the number to be calculated is: "))
      output = math.log(num, x)
      residue = output - int(output)
      if residue == 0:
        print (num, " is a power of the number desired.")
      else:
        print (num, " is not a power of the number desired.")
    y = checkPowTwo(int(input()))
    

提交回复
热议问题