-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathRussianPeasantMult.py
More file actions
63 lines (57 loc) · 1.98 KB
/
RussianPeasantMult.py
File metadata and controls
63 lines (57 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Russian Peasant Multiplication (RPM) Algorithm Implemented In Python
RPM is a method of mutiplication of any 2 numbers using only multiplication
and division by 2.
The basics are that you divide the second number by 2 (integer division) until
it equals 1, every time you divide the second number by 2, you also multiply
the first number by 2. After the second number hits 1, you then remove all right
side values that are even. Finally you add all of the left side values that are
left.
EXAMPLE:
10*5:
10 5 <== We add left side value (10) to total
10*2=20 5/2=2 <== We don't use this line since int(5/2) is an even number
20*2=40 2/2=1 <== We add left side value (40) to total
10 + 40 = 50
10 * 5 = 50
"""
import sys
def RPMult(x, y):
# Create an empty list to store values of each mult/div by 2
# Set total value to 0
total = 0
val_list =[]
count = 0
# If y is already equal to 1, return the value of x as total
if y != 1:
# Loop through calculations until y is equal to 1
while y >= 1:
# Add values of x and y to list
val_list.append((x, y))
# Division and multiplication by 2 on each side of equation
y = int(y/2)
x = x*2
else:
total = x
# Loop through list to calculate result of multiplication
for val in val_list:
# If the mod of val[1] returns anything other than 0, we add val[0] to
# the total result of the multiplication
if val[1]%2 >= 1:
total += val[0]
return total
def main():
arguments = len(sys.argv) - 1
if arguments != 2:
print("Error please pass two integer values!")
print("Usage: python RussianPeasantMult.py <int1> <int2>")
else:
try:
x = int(sys.argv[1])
y = int(sys.argv[2])
except ValueError:
print("Error: You can only pass integer values to program")
return
print(RPMult(x, y))
if __name__ == "__main__":
main()