Russian Peasant Multiplication
Small PowerShell exercise taken from the Programming Praxis: Russian Peasant Multiplication contest of WTF (Worse than Failure) implementing the Russian Peasant Multiplication method (for more info see original contest post)
function Invoke-RussianMultiply ([int]$a,[int]$b)
{
"$a x $b"
$r = 0
while($a -ne 1){
$a = $a/2
$b = $b*2
"$a x $b"
if ($a%2) {$r+=$b;"`t+$b"}
}
"result : $r"
}
The result looks like this :
PS C:\> Invoke-RussianMultiply 18 23
18 x 23
9 x 46
+46
4 x 92
2 x 184
1 x 368
+368
result : 414
Enjoy,
Greetings /\/\o\/\/