## DEV Community is a community of 750,997 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Scott Gordon

Posted on

# Russian Peasant Multiplication

``````# rpm.py
#   This program uses the "Russian Peasant Multiplication" algorithm
#   to solve for multiplication through binary expansion.
# by: Scott Gordon

import math
import pandas as pd

def main():
print('***** Russian Peasant Multiplication *****')
def rpm():
print('Input two comma separated numbers (x, y): ')
input_str = input('> ')
inputs = input_str.split(',')
n1, n2 = int(inputs[0]), int(inputs[1])

halving = [n1]
while(min(halving) > 1):
halving.append(math.floor(min(halving)/2))

doubling = [n2]
while(len(doubling) < len(halving)):
doubling.append(max(doubling) * 2)

half_double = pd.DataFrame(zip(halving,doubling))
rpm_out = half_double.to_markdown()
print('\n*** RPM Table ***')
print(f'{rpm_out}\n') # requires tabulate for md
half_double = half_double.loc[half_double[0]%2 == 1,:]