Skip to content

Instantly share code, notes, and snippets.

@msyvr
Created October 15, 2021 23:16
Show Gist options
  • Save msyvr/9fbb25c031cac5e4754c05158343cfcc to your computer and use it in GitHub Desktop.
Save msyvr/9fbb25c031cac5e4754c05158343cfcc to your computer and use it in GitHub Desktop.
Given a base, is a number a power of that base?
def ispowerof(n, base):
'''
if n is a power of b, return True, else return False
'''
test = False
if n % base == 0:
k = int(n / base)
for i in range(k+1):
print(i)
print(base**i)
if base**i == n:
print(f'Exponent = {i}')
test = True
break
return test
if __name__ == "__main__":
b = int(input("what positive integer base do you want to check for?:\n"))
n = int(input('Let\'s check if a number is a power of that base.\nWhat number?: '))
print(ispowerof(n, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment