Skip to content

Instantly share code, notes, and snippets.

@jashsu
Created August 19, 2013 20:28
Show Gist options
  • Save jashsu/6273713 to your computer and use it in GitHub Desktop.
Save jashsu/6273713 to your computer and use it in GitHub Desktop.
def is_power_of_three(num):
from types import IntType, FloatType
assert type(num) in (IntType, FloatType)
x = 3
if num in (0, 1): return True
elif num < 0: return False
elif num > 0 and num < 1:
while True:
y = 1.0/x #hacky..
#print y, x
if y == num: return True
elif y < num: return False
else: x = x*3
else:
while True:
if x == num: return True
elif x > num: return False
else: x = x*3
def test_is_power_of_three():
for i in (0,1,2,3,9,10,27,81,59049,123456789012,
-1,0.5,float(1.0/3),float(1.0/9),
'abc',012,True):
print i
try: print is_power_of_three(i)
except: print 'Type error'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment