Long multiplication: Difference between revisions
→{{header|Python}} (Trans Haskell): Simplified one expression in terms of reduce, pylinted, added {Works with}
(added Ol) |
(→{{header|Python}} (Trans Haskell): Simplified one expression in terms of reduce, pylinted, added {Works with}) |
||
Line 4,378:
Shorter version:
{{trans|Haskell}}
{{Works with|Python|3.7}}
<lang python>'''Long multiplication'''
from functools import reduce
def digits(x):
'''Digits of x as a list of integers.'''
return [int(c) for c in str(x)]
def mult_table(xs, ys):
'''Rows of all products.'''
return [[x * y for x in xs] for y in ys]
def polymul(xs, ys):
'''List of specific products.'''
return map(lambda *vs: sum(filter(None, vs)),▼
*[[0] * i + zs for i, zs in enumerate(mult_table(xs, ys))])▼
*[
[0] * i + zs for i, zs in
]
)
def longmult(x, y):
'''Long multiplication.'''
result = 0▼
return reduce(
for v in polymul(digits(x), digits(y)):▼
▲ return result
)
if __name__ ==
print
longmult(2 ** 64, 2 ** 64)
)</lang>
=={{header|Ol}}==
|