RosettaCodeData/Task/Ethiopian-multiplication/Python/ethiopian-multiplication-1.py

32 lines
745 B
Python
Raw Permalink Normal View History

2013-04-10 16:57:12 -07:00
tutor = True
def halve(x):
2015-11-18 06:14:39 +00:00
return x // 2
2013-04-10 16:57:12 -07:00
def double(x):
2015-11-18 06:14:39 +00:00
return x * 2
2013-04-10 16:57:12 -07:00
def even(x):
return not x % 2
def ethiopian(multiplier, multiplicand):
if tutor:
2015-11-18 06:14:39 +00:00
print("Ethiopian multiplication of %i and %i" %
(multiplier, multiplicand))
2013-04-10 16:57:12 -07:00
result = 0
while multiplier >= 1:
if even(multiplier):
2015-11-18 06:14:39 +00:00
if tutor:
print("%4i %6i STRUCK" %
(multiplier, multiplicand))
2013-04-10 16:57:12 -07:00
else:
2015-11-18 06:14:39 +00:00
if tutor:
print("%4i %6i KEPT" %
(multiplier, multiplicand))
2013-04-10 16:57:12 -07:00
result += multiplicand
multiplier = halve(multiplier)
multiplicand = double(multiplicand)
2015-11-18 06:14:39 +00:00
if tutor:
print()
2013-04-10 16:57:12 -07:00
return result