Austin Powers in Python

PythonServer Side ProgrammingProgramming

Suppose we have a number greater than 0, we have to check whether the number is power of two or not.

So, if the input is like 1024, then the output will be True.

To solve this, we will follow these steps −

  • while n > 1, do

    • n := n / 2

  • return true when n is same as 1, otherwise 0

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, n):
      while n > 1:
         n /= 2
      return n == 1

ob = Solution()
print(ob.solve(1024))

Input

1024

Output

True
raja
Published on 02-Sep-2020 13:04:33

Advertisements