We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 79daddf commit 8b06914Copy full SHA for 8b06914
exponen.py
@@ -0,0 +1,18 @@
1
+def conventional_exponentiation(a: int, b: int) -> int:
2
+ result = 1
3
+ for _ in range(b):
4
+ result *= a
5
+ return result
6
+def divide_and_conquer_exponentiation(a: int, b: int) -> int:
7
+ if b == 0:
8
+ return 1
9
+ elif b % 2 == 0:
10
+ half = divide_and_conquer_exponentiation(a, b // 2)
11
+ return half * half
12
+ else:
13
+ return a * divide_and_conquer_exponentiation(a, b - 1)
14
+base = 2
15
+exponent = 10
16
+print(f"Conventional Approach: {base}^{exponent} = {conventional_exponentiation(base, exponent)}")
17
+
18
+print(f"Divide & Conquer Approach: {base}^{exponent} = {divide_and_conquer_exponentiation(base, exponent)}")
0 commit comments