We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 36cd0de + 748bdb9 commit 2eb25bcCopy full SHA for 2eb25bc
Math/Russain Peasants Algorithm/python/russian-peasants-algorithm.py
@@ -0,0 +1,21 @@
1
+# Algorithm to find the product of two numbers in logarithmic time
2
+def multiply(a,b):
3
+ if a > b:
4
+ left = a
5
+ right = b
6
+ else:
7
+ left = b
8
+ right = a
9
+
10
+ prod = 0
11
12
+ while left > 0: # Loop till you reach 1
13
+ if left%2 != 0: # Add the value to product variable if the left column value is odd
14
+ prod = prod + right
15
16
+ left = left >> 1 # Right shift
17
+ right = right << 1 # Left Shift
18
19
+ return prod
20
21
+print(multiply(127,68))
0 commit comments