Anonymous user
Ethiopian multiplication: Difference between revisions
→{{header|Ruby}}
(→{{header|LOLCODE}}: Named functions is from the 1.3 proposal, not 1.2 spec) |
|||
Line 3,049:
Iterative and recursive implementations here.
I've chosen to highlight the example 20*5 which I think is more illustrative.
<lang ruby>def
def
# iterative
def
product = 0
while a >= 1
p [a, b,
product += b
a = halve(a)
b = double(b)
Line 3,066 ⟶ 3,065:
# recursive
def
return 0 if a < 1
p [a, b,
end
$DEBUG = true # $DEBUG also set to true if "-d" option given
a, b = 20, 5
puts "#{a} * #{b} = #{
{{out}}
<pre>
[20, 5, "STRIKE"]
[10, 10, "STRIKE"]
[5, 20, "KEEP"]
Line 3,100:
def test_rec6; assert_equal(0, rec_ethopian_multiply(0,5)); end
end</lang>
<pre>
# Running tests:
............
Finished tests in 0.
ruby -v: ruby 2.0.0p247 (2013-06-27) [i386-mingw32]
▲12 tests, 12 assertions, 0 failures, 0 errors</pre>
</pre>
=={{header|Scala}}==
|