Created
September 6, 2012 20:44
-
-
Save jonelf/3660240 to your computer and use it in GitHub Desktop.
Ethiopian Multiplication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ethiopian Multiplication | |
# usage: ruby em.rb 673 7 | |
m, n = ARGV.map(&:to_i) | |
product = 0 | |
while m >= 1 | |
puts "%4d : %4d %s" % [m, n, m.even? ? "Ignore" : ""] | |
product += n unless m.even? | |
m = m / 2 | |
n = n * 2 | |
end | |
puts "Product: #{product}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of output:
673 : 7
336 : 14 Ignore
168 : 28 Ignore
84 : 56 Ignore
42 : 112 Ignore
21 : 224
10 : 448 Ignore
5 : 896
2 : 1792 Ignore
1 : 3584
Product: 4711