Ethiopian multiplication: Difference between revisions

Content added Content deleted
(Added F# version)
No edit summary
Line 1,079: Line 1,079:


=={{header|Freebasic}}==
=={{header|Freebasic}}==
<lang freebasic>


<lang freebasic>
Function double_(y As String) As String
Function double_(y As String) As String
Var answer="0"+y
Var answer="0"+y
Line 1,092: Line 1,093:
Return Ltrim(answer,"0")
Return Ltrim(answer,"0")
End Function
End Function

Function Accumulate(NUM1 As String,NUM2 As String) As String
Function Accumulate(NUM1 As String,NUM2 As String) As String
Var three="0"+NUM1
Var three="0"+NUM1
Line 1,107: Line 1,108:
Return three
Return three
End Function
End Function

Function Half(Byref x As String) As String
Function Half(Byref x As String) As String
Var carry=0
Var carry=0
Line 1,119: Line 1,120:
Return x
Return x
End Function
End Function

Function IsOdd(x As String) As Integer
Function IsEven(x As String) As Integer
If x[Len(x)-1] And 1 Then Return -1
If x[Len(x)-1] And 1 Then Return 0
return -1
End Function
End Function

Function EthiopianMultiply(n1 As String,n2 As String) As String
Function EthiopianMultiply(n1 As String,n2 As String) As String
Dim As String x=n1,y=n2
Dim As String x=n1,y=n2
Line 1,132: Line 1,134:
End If
End If
Dim As String ans
Dim As String ans
Dim As String temprint
Dim As String temprint,odd
While x<>""
While x<>""
temprint=""
temprint=""
If IsOdd(x) Then
odd=""
temprint="*"
If not IsEven(x) Then
temprint=" *"
odd=" <-- odd"
ans=Accumulate(y,ans)
ans=Accumulate(y,ans)
End If
End If
Print x;tab(30);y;temprint
Print x;odd;tab(30);y;temprint
x=Half(x)
x=Half(x)
y= Double_(y)
y= Double_(y)
Line 1,147: Line 1,151:
'================= Example ====================
'================= Example ====================
Print
Print
Dim As String s1="123456789087654321"
Dim As String s1="17"
Dim As String s2="9876543210123456789"
Dim As String s2="34"
Print "Half";tab(30);"Double * marks those accumulated"
Print "Half";tab(30);"Double * marks those accumulated"
print "Biggest";tab(30);"Smallest"
print "Biggest";tab(30);"Smallest"


Print
Print

Var ans= EthiopianMultiply(s1,s2)
Var ans= EthiopianMultiply(s1,s2)

Print
Print
Print
Print
Print "Final answer and float check"
Print "Final answer"
Print " ";ans
Print " ";ans
print "Float check"
Print Val(s1)*Val(s2)
Print Val(s1)*Val(s2)

Sleep
Sleep

</lang>
</lang>
note: algorithm uses strings instead of integers

Output:
Output:
<pre>
<pre>
Line 1,171: Line 1,180:
Biggest Smallest
Biggest Smallest


9876543210123456789 123456789087654321*
34 17
17 <-- odd 34 *
4938271605061728394 246913578175308642
2469135802530864197 493827156350617284*
8 68
4 136
1234567901265432098 987654312701234568
617283950632716049 1975308625402469136*
2 272
308641975316358024 3950617250804938272
1 <-- odd 544 *
154320987658179012 7901234501609876544
77160493829089506 15802469003219753088
38580246914544753 31604938006439506176*
19290123457272376 63209876012879012352
9645061728636188 126419752025758024704
4822530864318094 252839504051516049408
2411265432159047 505679008103032098816*
1205632716079523 1011358016206064197632*
602816358039761 2022716032412128395264*
301408179019880 4045432064824256790528
150704089509940 8090864129648513581056
75352044754970 16181728259297027162112
37676022377485 32363456518594054324224*
18838011188742 64726913037188108648448
9419005594371 129453826074376217296896*
4709502797185 258907652148752434593792*
2354751398592 517815304297504869187584
1177375699296 1035630608595009738375168
588687849648 2071261217190019476750336
294343924824 4142522434380038953500672
147171962412 8285044868760077907001344
73585981206 16570089737520155814002688
36792990603 33140179475040311628005376*
18396495301 66280358950080623256010752*
9198247650 132560717900161246512021504
4599123825 265121435800322493024043008*
2299561912 530242871600644986048086016
1149780956 1060485743201289972096172032
574890478 2120971486402579944192344064
287445239 4241942972805159888384688128*
143722619 8483885945610319776769376256*
71861309 16967771891220639553538752512*
35930654 33935543782441279107077505024
17965327 67871087564882558214155010048*
8982663 135742175129765116428310020096*
4491331 271484350259530232856620040192*
2245665 542968700519060465713240080384*
1122832 1085937401038120931426480160768
561416 2171874802076241862852960321536
280708 4343749604152483725705920643072
140354 8687499208304967451411841286144
70177 17374998416609934902823682572288*
35088 34749996833219869805647365144576
17544 69499993666439739611294730289152
8772 138999987332879479222589460578304
4386 277999974665758958445178921156608
2193 555999949331517916890357842313216*
1096 1111999898663035833780715684626432
548 2223999797326071667561431369252864
274 4447999594652143335122862738505728
137 8895999189304286670245725477011456*
68 17791998378608573340491450954022912
34 35583996757217146680982901908045824
17 71167993514434293361965803816091648*
8 142335987028868586723931607632183296
4 284671974057737173447863215264366592
2 569343948115474346895726430528733184
1 1138687896230948693791452861057466368*




Final answer and float check
Final answer
578
1219326312007315957660722452012635269
Float check
1.219326312007316e+036
578