Anonymous user
Ethiopian multiplication: Difference between revisions
doh, CoffeeScript comes before ColdFusion
(CoffeeScript) |
(doh, CoffeeScript comes before ColdFusion) |
||
Line 579:
(recur (halve a) (twice b) r)
(recur (halve a) (twice b) (+ r b))))))</lang>
=={{header|CoffeeScript}}==▼
<lang coffeescript>▼
halve = (n) -> Math.floor n / 2▼
double = (n) -> n * 2▼
is_even = (n) -> n % 2 == 0▼
multiply = (a, b) ->▼
prod = 0▼
while a > 0▼
prod += b if !is_even a▼
a = halve a▼
b = double b▼
prod▼
# tests▼
do ->▼
for i in [0..100]▼
for j in [0..100]▼
throw Error("broken for #{i} * #{j}") if multiply(i,j) != i * j▼
</lang>▼
=={{header|ColdFusion}}==
Line 690 ⟶ 713:
...equals 578
</pre>
▲=={{header|CoffeeScript}}==
▲<lang coffeescript>
▲halve = (n) -> Math.floor n / 2
▲double = (n) -> n * 2
▲is_even = (n) -> n % 2 == 0
▲multiply = (a, b) ->
▲ prod = 0
▲ while a > 0
▲ prod += b if !is_even a
▲ a = halve a
▲ b = double b
▲ prod
▲# tests
▲do ->
▲ for i in [0..100]
▲ for j in [0..100]
▲ throw Error("broken for #{i} * #{j}") if multiply(i,j) != i * j
▲</lang>
=={{header|Common Lisp}}==
|