diff --git a/math/README.md b/math/README.md new file mode 100644 index 0000000..548d162 --- /dev/null +++ b/math/README.md @@ -0,0 +1,9 @@ +## Power function + +# What it does +Given a base and power using recursion, calculates the result by multiplying the base by the base to the power of power minus one. + +# How to use +Run the program +Type in your base +Type in your power \ No newline at end of file diff --git a/math/power.py b/math/power.py new file mode 100644 index 0000000..405ffeb --- /dev/null +++ b/math/power.py @@ -0,0 +1,16 @@ +def ofun_power(base:int, power:int): + # Base case once power reaches 0 function will stop + if power == 0: + return 1 + + if power > 0: + return base * ofun_power(base, power - 1) + # This will be used if the power if negative resulting in a fraction + elif power < 0: + return (1/base) * ofun_power(base, power + 1) + + +user_base = input("Input base: ") +user_power = input("Input power: ") + +resulted_power = ofun_power(user_base, user_power) \ No newline at end of file