Skip to content

Commit 2eb25bc

Browse files
Merge pull request matthewsamuel95#604 from melwinalm/master
Added Russian Peasants Algorithm
2 parents 36cd0de + 748bdb9 commit 2eb25bc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)