RosettaCodeData/Task/Ethiopian-multiplication/PowerShell/ethiopian-multiplication-2.psh
Ingy döt Net d066446780 langs a-z
2013-04-10 22:43:41 -07:00

29 lines
504 B
Text

function halveInt( [int] $rhs )
{
[math]::floor( $rhs / 2 )
}
function doubleInt( [int] $rhs )
{
$rhs*2
}
function isEven( [int] $rhs )
{
-not ( $_ % 2 )
}
function Ethiopian( [int] $lhs , [int] $rhs )
{
$scratch = @{}
1..[math]::floor( [math]::log( $lhs , 2 ) + 1 ) |
ForEach-Object {
$scratch[$lhs] = $rhs
$lhs
$lhs = halveInt( $lhs )
$rhs = doubleInt( $rhs ) } |
Where-Object { -not ( isEven $_ ) } |
ForEach-Object { $sum = 0 } { $sum += $scratch[$_] } { $sum }
}
Ethiopian 17 34