Anonymous user
Ethiopian multiplication: Difference between revisions
→{{header|PureBasic}}: changed isOdd() to isEven()
(→{{header|PowerShell}}: Added PureBasic) |
(→{{header|PureBasic}}: changed isOdd() to isEven()) |
||
Line 1,356:
multiplyValues 17 34</lang>
=={{header|PureBasic}}==
<lang PureBasic>Procedure
ProcedureReturn (x & 1) ! 1
EndProcedure
Line 1,372:
Print("Ethiopian multiplication of " + Str(x) + " and " + Str(y) + " ... ")
Repeat
If
sum + y
EndIf
Line 1,392:
<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.
<lang PureBasic>Procedure
ProcedureReturn (x & 1) ! 1
EndProcedure
Line 1,409:
Print("Ethiopian multiplication of " + Str(x) + " and " + Str(y) + " ...")
Repeat
If
sum + y
EndIf
x = halveValue(x)
|