Skip to content

Added Exponent Finding program #2236

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions maths/exponent_recursion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'''
=============================== Finding Exponent using Recursion =======================

i/p -->
Enter the base: 3
Enter the exponent: 4
o/p -->
3 to the power of 4: 81


i/p -->
Enter the base: 2
Enter the exponent: 0
o/p -->
2 to the power of 0: 1
'''
def power(base: int, exponent: int)->int:
"""
>>> all(power(base, exponent) == pow(base, exponent)
... for base in range(-10, 10) for exponent in range(10))
True
"""
return base * power(base, exponent - 1) if exponent > 0 else 1
if(p == 0):
return 1
else:
return (n * powerCalculation(n, (p-1)))

if __name__ == "__main__":
n = int(input("Enter the base: "))
p = int(input("Enter the exponent: "))
result = powerCalculation(n, p)
print("{} to the power of {}: {}".format(n, p, result))