Skip to content

Instantly share code, notes, and snippets.

@bondnotanymore
Last active October 20, 2017 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bondnotanymore/9afc91cb1730c9b9f637326fafc69371 to your computer and use it in GitHub Desktop.
Save bondnotanymore/9afc91cb1730c9b9f637326fafc69371 to your computer and use it in GitHub Desktop.
import sys
def if_powerof(n,x):
var = n
power = 0
if n%x == 0:
while var > 1:
if var % x == 0:
var = var/x
power+= 1
else:
return "{} is not a power of {}, but just a multiple of 3".format(n, x)
#else clause on the while loop; this comes into play when your loop has finished running without any break;
else:
return "{} is a number, which is {}th power of {}".format(n, power, x)
else:
return "{} is not even a multiple of {}".format(n, x)
print if_powerof(729, 9)
print if_powerof(216, 3)
print if_powerof(243, 3)
print if_powerof(11748, 8)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment