Ethiopian multiplication: Difference between revisions

Content added Content deleted
(→‎{{header|PowerShell}}: Added PureBasic)
(→‎{{header|PureBasic}}: changed isOdd() to isEven())
Line 1,356: Line 1,356:
multiplyValues 17 34</lang>
multiplyValues 17 34</lang>
=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure isOdd(x)
<lang PureBasic>Procedure isEven(x)
ProcedureReturn x & 1
ProcedureReturn (x & 1) ! 1
EndProcedure
EndProcedure


Line 1,372: Line 1,372:
Print("Ethiopian multiplication of " + Str(x) + " and " + Str(y) + " ... ")
Print("Ethiopian multiplication of " + Str(x) + " and " + Str(y) + " ... ")
Repeat
Repeat
If isOdd(x)
If Not isEven(x)
sum + y
sum + y
EndIf
EndIf
Line 1,392: Line 1,392:
<pre>Ethiopian multiplication of 17 and 34 ... equals 578</pre>
<pre>Ethiopian multiplication of 17 and 34 ... equals 578</pre>
It became apparent that according to the way the Ethiopian method is described above it can't produce a correct result if the first multiplicand (the one being repeatedly halved) is negative. I've addressed that in this variation. If the first multiplicand is negative then the resulting sum (which may already be positive or negative) is negated.
It became apparent that according to the way the Ethiopian method is described above it can't produce a correct result if the first multiplicand (the one being repeatedly halved) is negative. I've addressed that in this variation. If the first multiplicand is negative then the resulting sum (which may already be positive or negative) is negated.
<lang PureBasic>Procedure isOdd(x)
<lang PureBasic>Procedure isEven(x)
ProcedureReturn x & 1
ProcedureReturn (x & 1) ! 1
EndProcedure
EndProcedure


Line 1,409: Line 1,409:
Print("Ethiopian multiplication of " + Str(x) + " and " + Str(y) + " ...")
Print("Ethiopian multiplication of " + Str(x) + " and " + Str(y) + " ...")
Repeat
Repeat
If isOdd(x)
If Not isEven(x)
sum + y
sum + y
Else
EndIf
EndIf
x = halveValue(x)
x = halveValue(x)