From 2805009a06abd1f015f59fd828d3a1870a0b458b Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Sun, 26 Jul 2020 23:22:53 +0530 Subject: [PATCH 1/8] Finding Exponent Program --- maths/exponent_recursion.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 maths/exponent_recursion.py diff --git a/maths/exponent_recursion.py b/maths/exponent_recursion.py new file mode 100644 index 000000000000..29e676a8e612 --- /dev/null +++ b/maths/exponent_recursion.py @@ -0,0 +1,36 @@ +""" + =============================== 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 powerCalculation(n: int, p: int) -> int: + 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, abs(p)) + if p < 0: + result = 1 / result + print("{} to the power of {}: {}".format(n, p, result)) + else: + print("{} to the power of {}: {}".format(n, p, result)) From f863adaecb05af9fef27dd779044ca272d0e3e83 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Sun, 26 Jul 2020 23:59:07 +0530 Subject: [PATCH 2/8] Build Error Fix - 1 --- maths/exponent_recursion.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/exponent_recursion.py b/maths/exponent_recursion.py index 29e676a8e612..f4a58c400a62 100644 --- a/maths/exponent_recursion.py +++ b/maths/exponent_recursion.py @@ -17,18 +17,18 @@ """ -def powerCalculation(n: int, p: int) -> int: +def power(n: int, p: int) -> int: if p == 0: return 1 else: - return n * powerCalculation(n, (p - 1)) + return n * power(n, (p - 1)) if __name__ == "__main__": n = int(input("Enter the base: ")) p = int(input("Enter the exponent: ")) - result = powerCalculation(n, abs(p)) + result = power(n, abs(p)) if p < 0: result = 1 / result print("{} to the power of {}: {}".format(n, p, result)) From 9ebeb8f87506434bfb6330d75fc8067751680e3d Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Mon, 27 Jul 2020 00:13:57 +0530 Subject: [PATCH 3/8] Build Error Fix - 2 --- maths/exponent_recursion.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/maths/exponent_recursion.py b/maths/exponent_recursion.py index f4a58c400a62..993840503bd6 100644 --- a/maths/exponent_recursion.py +++ b/maths/exponent_recursion.py @@ -1,17 +1,13 @@ """ - =============================== Finding Exponent using Recursion ======================= - + == 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 """ From 007e848d6590b15acb5cf1a8be211edcee975f7a Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Mon, 27 Jul 2020 00:28:21 +0530 Subject: [PATCH 4/8] Error Fix - 1 datatype --- maths/exponent_recursion.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/exponent_recursion.py b/maths/exponent_recursion.py index 993840503bd6..86bf88b013e5 100644 --- a/maths/exponent_recursion.py +++ b/maths/exponent_recursion.py @@ -26,7 +26,7 @@ def power(n: int, p: int) -> int: result = power(n, abs(p)) if p < 0: - result = 1 / result - print("{} to the power of {}: {}".format(n, p, result)) + newResult = 1 / result + print("{} to the power of {}: {}".format(n, p, newResult)) else: print("{} to the power of {}: {}".format(n, p, result)) From 7ec8e17ae19cb25574e3f41ce27e4815db08cc2d Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Mon, 27 Jul 2020 02:54:38 +0530 Subject: [PATCH 5/8] self-documenting naming convension added --- maths/exponent_recursion.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/maths/exponent_recursion.py b/maths/exponent_recursion.py index 86bf88b013e5..fabc6efd2f45 100644 --- a/maths/exponent_recursion.py +++ b/maths/exponent_recursion.py @@ -13,20 +13,20 @@ """ -def power(n: int, p: int) -> int: - if p == 0: +def power(base: int, exponent: int) -> int: + if exponent == 0: return 1 else: - return n * power(n, (p - 1)) + return base * power(base, (exponent - 1)) if __name__ == "__main__": - n = int(input("Enter the base: ")) - p = int(input("Enter the exponent: ")) + base = int(input("Enter the base: ")) + exponent = int(input("Enter the exponent: ")) - result = power(n, abs(p)) - if p < 0: + result = power(base, abs(exponent)) + if exponent < 0: newResult = 1 / result - print("{} to the power of {}: {}".format(n, p, newResult)) + print("{} to the power of {}: {}".format(base, exponent, newResult)) else: - print("{} to the power of {}: {}".format(n, p, result)) + print("{} to the power of {}: {}".format(base, exponent, result)) From 7fed6a13cd86b577ff950cfff6387a01129ea194 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 27 Jul 2020 11:17:32 +0200 Subject: [PATCH 6/8] Update and rename exponent_recursion.py to power_using_recursion.py --- maths/exponent_recursion.py | 32 ------------------------------ maths/power_using_recursion.py | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 32 deletions(-) delete mode 100644 maths/exponent_recursion.py create mode 100644 maths/power_using_recursion.py diff --git a/maths/exponent_recursion.py b/maths/exponent_recursion.py deleted file mode 100644 index fabc6efd2f45..000000000000 --- a/maths/exponent_recursion.py +++ /dev/null @@ -1,32 +0,0 @@ -""" - == 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: - if exponent == 0: - return 1 - else: - return base * power(base, (exponent - 1)) - - -if __name__ == "__main__": - base = int(input("Enter the base: ")) - exponent = int(input("Enter the exponent: ")) - - result = power(base, abs(exponent)) - if exponent < 0: - newResult = 1 / result - print("{} to the power of {}: {}".format(base, exponent, newResult)) - else: - print("{} to the power of {}: {}".format(base, exponent, result)) diff --git a/maths/power_using_recursion.py b/maths/power_using_recursion.py new file mode 100644 index 000000000000..068c834ead46 --- /dev/null +++ b/maths/power_using_recursion.py @@ -0,0 +1,36 @@ +""" +== 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: 81 + Inout --> + Enter the base: 2 + Enter the exponent: 0 + Output --> + 2 to the power of 0: 1 +""" + + +def power(base: int, exponent: int) -> float: + """ + 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 + """ + return base * power(base, (exponent - 1)) if exponent else 1 + + +if __name__ == "__main__": + print("Raise base to the power of exponent using recursion...") + base = int(input("Enter the base: ").strip()) + exponent = int(input("Enter the exponent: ").strip()) + result = power(base, abs(exponent)) + if exponent < 0: # power() does not properly deal w/ negative exponents + result = 1 / result + print(f"{base} to the power of {exponent} is {result}" From 0f258db5ae43443b840d83c3dfafb4a4d210629e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 27 Jul 2020 11:23:52 +0200 Subject: [PATCH 7/8] Fix typo --- maths/power_using_recursion.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/power_using_recursion.py b/maths/power_using_recursion.py index 068c834ead46..68ab89787538 100644 --- a/maths/power_using_recursion.py +++ b/maths/power_using_recursion.py @@ -5,7 +5,7 @@ Enter the exponent: 4 Output --> 3 to the power of 4: 81 - Inout --> + Input --> Enter the base: 2 Enter the exponent: 0 Output --> @@ -33,4 +33,4 @@ def power(base: int, exponent: int) -> float: result = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents result = 1 / result - print(f"{base} to the power of {exponent} is {result}" + print(f"{base} to the power of {exponent} is {result}") From 779e6d1ebec1c32810f69b6c37464e28cf0cd9c1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 27 Jul 2020 11:32:34 +0200 Subject: [PATCH 8/8] Fix typo --- maths/power_using_recursion.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/power_using_recursion.py b/maths/power_using_recursion.py index 68ab89787538..f82097f6d8ec 100644 --- a/maths/power_using_recursion.py +++ b/maths/power_using_recursion.py @@ -4,12 +4,12 @@ Enter the base: 3 Enter the exponent: 4 Output --> - 3 to the power of 4: 81 + 3 to the power of 4 is 81 Input --> Enter the base: 2 Enter the exponent: 0 Output --> - 2 to the power of 0: 1 + 2 to the power of 0 is 1 """