Skip to content

[ADD]: Improved tests in power recursion! #10664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 20, 2023
41 changes: 41 additions & 0 deletions maths/power_recursion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
== Raise base to the power of exponent using recursion ==
Input -->
Enter the base: 3
Enter the exponent: 4
Output -->
3 to the power of 4 is 81
Input -->
Enter the base: 2
Enter the exponent: 0
Output -->
2 to the power of 0 is 1
"""


def power(base: int, exponent: int) -> float:
"""
Calculate the power of a base raised to an exponent.

Examples:
>>> power(3, 4)
81
>>> power(2, 0)
1
>>> all(power(base, exponent) == pow(base, exponent)
... for base in range(-10, 10) for exponent in range(10))
True
"""
if exponent == 0:
return 1
elif exponent < 0:
return 1 / power(base, -exponent)
else:
return base * power(base, exponent - 1)


if __name__ == "__main__":
base = int(input("Enter the base: ").strip())
exponent = int(input("Enter the exponent: ").strip())
result = power(base, exponent)
print(f"{base} to the power of {exponent} is {result}")
53 changes: 0 additions & 53 deletions maths/power_using_recursion.py

This file was deleted.