- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
class Solution: def solve(self, n): while n > 1: n /= 2 return n == 1 ob = Solution() print(ob.solve(1024))
Input
1024
Output
True
Advertisements