Ethiopian multiplication: Difference between revisions

m
 
(7 intermediate revisions by 6 users not shown)
Line 51:
Use these functions to '''create a function that does Ethiopian multiplication'''.
 
;Related tasks:
* [[Egyptian_division|Egyptian division]]
 
;References:
Line 975 ⟶ 977:
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
Same code as [[#Nascom_BASIC|Nascom BASIC]]
 
==={{header|ASIC}}===
Line 1,067 ⟶ 1,071:
FUNCTION isEven% (a AS INTEGER)
isEven% = (a MOD 2) - 1
END FUNCTION</syntaxhighlight>{{out}}
{{out}}
17 34
<pre> 17 34
8
4
2
1 544
= 578</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256vbnet">outP = 0
x = 17
y = 34
Line 1,129 ⟶ 1,134:
DEF FNhalve(A%) = A% DIV 2
DEF FNeven(A%) = ((A% AND 1) = 0)</syntaxhighlight>{{out}}
{{out}}
17 34
<pre> 17 34
8 ---
4 ---
Line 1,136 ⟶ 1,142:
1 544
===
578</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|BASIC256}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 sub doub(a)
110 doub = a*2
120 end sub
130 sub half(a)
140 half = int(a/2)
150 end sub
160 sub iseven(a)
170 iseven = (a mod 2)-1
180 end sub
190 outp = 0
200 x = 17
210 y = 34
220 while 1
230 print x;chr$(9);
240 if not (iseven(x)) then
250 outp = outp - y
260 print y
270 else
280 print
290 endif
300 if x < 2 then exit while
310 x = half(x)
320 y = doub(y)
330 wend
340 print "=";chr$(9);outp
350 end</syntaxhighlight>
 
==={{header|FreeBASIC}}===
Line 1,227 ⟶ 1,263:
Sleep
</syntaxhighlight>note: algorithm uses strings instead of integers
{{out}}
{{out}}<pre>Half Double * marks those accumulated
<pre>Half Double * marks those accumulated
Biggest Smallest
 
Line 1,296 ⟶ 1,333:
doubleInt = Int(num * 2)
End Function</syntaxhighlight>
 
 
==={{header|Microsoft Small Basic}}===
<syntaxhighlight lang="microsoftsmallbasic">x = 17
x = 17
y = 34
tot = 0
Line 1,317 ⟶ 1,352:
TextWindow.Write("=")
TextWindow.CursorLeft = 10
TextWindow.WriteLine(tot)</syntaxhighlight>
</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
<syntaxhighlight lang="gwbasic">10 REM Ethiopian multiplication
10 REM Ethiopian multiplication
20 DEF FND(A) = 2*A
30 DEF FNH(A) = INT(A/2)
Line 1,340 ⟶ 1,373:
170 PRINT "------------"
180 PRINT "= "; TAB(9); T; "(sum of kept second vals)"
190 END</syntaxhighlight>
 
</syntaxhighlight>
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
Same code as [[#Nascom_BASIC|Nascom BASIC]]
 
==={{header|Nascom BASIC}}===
{{trans|Modula-2}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="basic">10 REM Ethiopian multiplication
10 REM Ethiopian multiplication
20 DEF FND(A)=2*A
30 DEF FNH(A)=INT(A/2)
Line 1,367 ⟶ 1,402:
1000 S$=STR$(NR)
1010 PRINT SPC(9-LEN(S$));S$;
1020 RETURN</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre> 17 34
17 34
8
4
2
1 544
= 578</pre>
</pre>
 
==={{header|PureBasic}}===
Line 1,455 ⟶ 1,487:
EndIf</syntaxhighlight>
{{out}}
<pre> Ethiopian multiplication of 17 and 34 ... equals 578
Ethiopian multiplication of -17 and 34 ... equals -578
Ethiopian multiplication of -17 and -34 ... equals 578</pre>
 
==={{header|QB64}}===
Line 1,486 ⟶ 1,518:
END FUNCTION</syntaxhighlight>
{{out}}
<pre>578</pre>
578
</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
Line 1,579 ⟶ 1,609:
REM A - param.; E - result (0 - false)
400 LET E=A-(A/2)*2
RETURN </syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>17, 34 (kept)
17, 34 (kept)
8, 68
4, 136
Line 1,589 ⟶ 1,617:
1, 544 (kept)
------------
= 578 (sum of kept second vals)</pre>
</pre>
 
==={{header|True BASIC}}===
A translation of BBC BASIC. True BASIC does not have Boolean operations built-in.
<syntaxhighlight lang="basic">!RosettaCode: Ethiopian Multiplication
!RosettaCode: Ethiopian Multiplication
! True BASIC v6.007
PROGRAM EthiopianMultiplication
Line 1,623 ⟶ 1,649:
DEF FNhalve(A) = INT(A / 2)
DEF FNeven(A) = MOD(A+1,2)
END</syntaxhighlight>
END
 
</syntaxhighlight>
 
==={{header|XBasic}}===
{{trans|Modula-2}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">' Ethiopian multiplication
' Ethiopian multiplication
PROGRAM "ethmult"
VERSION "0.0000"
Line 1,671 ⟶ 1,694:
RETURN a&& MOD 2 = 0
END FUNCTION
END PROGRAM</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre> 17 34
17 34
8
4
2
1 544
= 578</pre>
</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasicvbnet">outP = 0
x = 17
y = 34
Line 2,374 ⟶ 2,394:
 
print "="
print t</syntaxhighlight>
{{out| Output}}<pre>17 34
 
8
end</syntaxhighlight>
4
2
1
1 544
=
578</pre>
 
=={{header|D}}==
Line 2,481 ⟶ 2,507:
 
=={{header|EasyLang}}==
<syntaxhighlight>
{{trans|Microsoft Small Basic}}
func mult x y .
<syntaxhighlight lang="text">
while x >= 171
if x mod 2 <> 0
y = 34
tot += 0y
.
while x >= 1
write x = x &div "\t"2
if (x + 1) mod 2y *= 02
tot += y.
return print ytot
else
print ""
.
x = x div 2
y = 2 * y
.
print "=\t"mult &17 tot34
</syntaxhighlight>
 
Line 2,848 ⟶ 2,869:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Ancient_Egyptian_multiplication}}
 
'''Solution'''
 
[[File:Fōrmulæ - Ancient Egyptian multiplication 01.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Ancient Egyptian multiplication 02.png]]
 
[[File:Fōrmulæ - Ancient Egyptian multiplication 03.png]]
 
Because the required functions are either simple or intrinsic, the solution can be much simpler:
 
[[File:Fōrmulæ - Ancient Egyptian multiplication 04.png]]
 
=={{header|FutureBasic}}==
Line 3,768 ⟶ 3,803:
 
578
</syntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function to halve */
halve(n):=floor(n/2)$
 
/* Function to double */
double(n):=2*n$
 
/* Predicate function to check wether an integer is even */
my_evenp(n):=if mod(n,2)=0 then true$
 
/* Function that implements ethiopian function using the three previously defined functions */
ethiopian(n1,n2):=block(cn1:n1,cn2:n2,list_w:[],
while cn1>0 do (list_w:endcons(cn1,list_w),cn1:halve(cn1)),
n2_list:append([cn2],makelist(cn2:double(cn2),length(list_w)-1)),
sublist_indices(list_w,lambda([x],not my_evenp(x))),
makelist(n2_list[i],i,%%),
apply("+",%%))$
</syntaxhighlight>
 
Line 6,604 ⟶ 6,659:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var halve = Fn.new { |n| (n/2).truncate }
 
var double = Fn.new { |n| n * 2 }
1,990

edits