## the russian peasants are multiplying!

Via this post, I found out about Russian Peasant Multiplication, a rather clever method of multiplication that only requires, doubling, halving and adding. So I wrote some code to display it:

```#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
results=[]
indicator=' '

left=int(sys.argv[1])
right=int(sys.argv[2])

while right >= 1:
Â Â Â  indicator='X'
Â Â Â  if right % 2:
Â Â Â Â Â Â Â  indicator=' 'Â Â Â Â Â Â Â Â Â Â Â Â Â  # right number is odd,
Â Â Â Â Â Â Â  results.append(left)Â Â Â Â Â Â  #Â  so add left number to results
Â Â Â  print (" %s %16d \t %16d %s") % (indicator, left, right, indicator)
Â Â Â  left *= 2
Â Â Â  right /= 2

print("%s Ã— %s = %s = %d")%(sys.argv[1], sys.argv[2],
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  ' + '.join(map(str,results)), sum(results))```

So to multiply 571Â Ã— 293:

```\$ ./rpmult.py 571 293
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  571 Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â Â  293 Â
Â XÂ Â Â Â Â Â Â Â Â Â Â Â  1142 Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â Â  146 X
Â Â Â Â Â Â Â Â Â Â Â Â Â Â  2284 Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  73 Â
Â XÂ Â Â Â Â Â Â Â Â Â Â Â  4568 Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  36 X
Â XÂ Â Â Â Â Â Â Â Â Â Â Â  9136 Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  18 X
Â Â Â Â Â Â Â Â Â Â Â Â Â  18272 Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  9 Â
Â XÂ Â Â Â Â Â Â Â Â Â Â  36544 Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  4 X
Â XÂ Â Â Â Â Â Â Â Â Â Â  73088 Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  2 X
Â Â Â Â Â Â Â Â Â Â Â Â  146176 Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  1 Â
571 Ã— 293 = 571 + 2284 + 18272 + 146176 = 167303```

Python’s still got some weirdness compared to Perl; where I’d join the list of sum terms in Perl with `join(' + ', @results)`, in Python you have to convert the integer values to strings, then call the join method of the separator string: `' + '.join(map(str,results))`. Still, I’ll give Python props for having a built-in list `sum()` function, which Perl lacks.