How to resolve the algorithm Ethiopian multiplication step by step in the PowerShell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Ethiopian multiplication step by step in the PowerShell programming language
Table of Contents
Problem Statement
Ethiopian multiplication is a method of multiplying integers using only addition, doubling, and halving.
Method:
For example: 17 × 34 Halving the first column: Doubling the second column: Strike-out rows whose first cell is even: Sum the remaining numbers in the right-hand column: So 17 multiplied by 34, by the Ethiopian method is 578.
The task is to define three named functions/methods/procedures/subroutines:
Use these functions to create a function that does Ethiopian multiplication.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ethiopian multiplication step by step in the PowerShell programming language
Source code in the powershell programming language
function isEven {
param ([int]$value)
return [bool]($value % 2 -eq 0)
}
function doubleValue {
param ([int]$value)
return [int]($value * 2)
}
function halveValue {
param ([int]$value)
return [int]($value / 2)
}
function multiplyValues {
param (
[int]$plier,
[int]$plicand,
[int]$temp = 0
)
while ($plier -ge 1)
{
if (!(isEven $plier)) {
$temp += $plicand
}
$plier = halveValue $plier
$plicand = doubleValue $plicand
}
return $temp
}
multiplyValues 17 34
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
You may also check:How to resolve the algorithm Character codes step by step in the Frink programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Secure temporary file step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Pell's equation step by step in the Python programming language
You may also check:How to resolve the algorithm Parsing/RPN to infix conversion step by step in the AWK programming language