rosetta: ethiopian_multiplication
authorRenata Carvalho <renatawm@gmail.com>
Thu, 11 Jun 2015 15:05:36 +0000 (11:05 -0400)
committerJean Privat <jean@pryen.org>
Tue, 23 Jun 2015 00:22:11 +0000 (20:22 -0400)
Close #1464

Signed-off-by: Renata Carvalho <renatawm@gmail.com>

examples/rosettacode/ethiopian_multiplication.nit [new file with mode: 0644]
tests/ethiopian_multiplication.inputs [new file with mode: 0644]
tests/sav/ethiopian_multiplication.res [new file with mode: 0644]

diff --git a/examples/rosettacode/ethiopian_multiplication.nit b/examples/rosettacode/ethiopian_multiplication.nit
new file mode 100644 (file)
index 0000000..0e7f848
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Ethiopian Multiplication
+# SEE: <http://rosettacode.org/wiki/ethiopian_multiplication>
+#
+# Generic non-robust version.
+module ethiopian_multiplication
+
+fun ethiopian(x, y: Int): Int
+do
+       print "{x}; {0}"
+       var sum: Int
+       if x.is_even then
+               sum = 0
+       else
+               sum = y
+       end
+
+       while x>1 do
+               x /= 2
+               y *= 2
+               print "{x}; {y}"
+               if not x.is_even then sum += y
+       end
+
+       return sum
+end
+
+var words = gets.split(" ")
+print ethiopian(words[0].to_i, words[1].to_i)
diff --git a/tests/ethiopian_multiplication.inputs b/tests/ethiopian_multiplication.inputs
new file mode 100644 (file)
index 0000000..25bf134
--- /dev/null
@@ -0,0 +1 @@
+8 6
diff --git a/tests/sav/ethiopian_multiplication.res b/tests/sav/ethiopian_multiplication.res
new file mode 100644 (file)
index 0000000..1f2aa01
--- /dev/null
@@ -0,0 +1,5 @@
+8; 0
+4; 12
+2; 24
+1; 48
+48