Austin Powers in Python


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

Updated on: 02-Sep-2020

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements