Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created September 6, 2012 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonelf/3660240 to your computer and use it in GitHub Desktop.
Save jonelf/3660240 to your computer and use it in GitHub Desktop.
Ethiopian Multiplication
# 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}"
@jonelf
Copy link
Author

jonelf commented Sep 6, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment