Ethiopian multiplication: Difference between revisions

(Undo revision 94268 by 161.49.249.254 (talk) Doesn't use the three named functions as required in the task description)
Line 1,733:
 
=={{header|PowerShell}}==
===Traditional===
<lang PowerShell>function isEven {
param ([int]$value)
Line 1,768 ⟶ 1,769:
 
multiplyValues 17 34</lang>
===Pipes with Busywork===
This uses several PowerShell specific features, in functions everything is returned automatically, so explicitly stating return is unnecessary. type conversion happens automatically for certain types, [int] into [boolean] maps 0 to false and everything else to true. A hash is used to store the values as they are being written, then a pipeline is used to iterate over the keys of the hash, determine which are odd, and only sum those. The three-valued ForEach-Object is used to set a start expression, an iterative expression, and a return expression.
<lang PowerShell>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
</lang>
 
=={{header|Powerbuilder}}==
<lang powerbuilder>public function boolean wf_iseven (long al_arg);return mod(al_arg, 2 ) = 0
Anonymous user