Power of Two
Given an integer, write a function to determine if it is a power of two. [#231]
Examples: Input: 1 Output: true Input: 0 Output: false Input: 4 Output: true Input: 64 Output: false
>>> PowerOfTwo = lambda n:bin(n).count('1')==1 >>> PowerOfTwo(1) True >>> PowerOfTwo(0) False >>> PowerOfTwo(4) True >>> PowerOfTwo(64) True >>>
Power of Three
Given an integer, write a function to determine if it is a power of three. [#326]
Examples: Input: 27 Output: true Input: 0 Output: false Input: 9 Output: true Input: 45 Output: false
Follow up:
Could you do it without using any loop / recursion?
>>> PowerofThree = lambda n:bin(int(n**(2/3))).count('1')==1 >>> PowerofThree(27) True >>> PowerofThree(0) False >>> PowerofThree(9) True >>> PowerofThree(45) False