Jump to content

Ethiopian multiplication: Difference between revisions

Add Python example showing off more of the language's key features
(Add Python example showing off more of the language's key features)
Line 2,348:
 
return result</lang>
 
Using generators, imap, and a generator expression.
 
<lang python>from itertools import izip
 
def halver(x):
while x >= 1:
yield x
x /= 2
 
def doubler(x):
while 1:
yield x
x *= 2
 
def even(x):
return x % 2 == 0
 
def ethiopian(x, y):
return sum(q for p, q in izip(halver(x), doubler(y)) if not even(p))</lang>
 
=={{header|R}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.