Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def LowestRootPwr():
'''
This function prompts the user to enter an int.
Returns: Tuple of two ints, root and pwr, such that
root**pwr == userInput, while adhering to the
constraints: 0 < pwr < 6,
root and pwr have the smallest
possible absolute values.
'''
while True:
try:
userInput = int(input("Enter a number: "))
break
except ValueError:
print("Value entered not an integer.")
rootLoopStep = 1
pwrLoopStep = 1
if userInput < 0:
rootLoopStep = -1
pwrLoopStep = 2
for root in range(0, userInput + rootLoopStep, rootLoopStep):
for pwr in range(1, 6, pwrLoopStep):
result = root**pwr
if result == userInput:
return (root, pwr)
elif abs(result) > abs(userInput):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.