Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
2
Task/Long-multiplication/Python/long-multiplication-1.py
Normal file
2
Task/Long-multiplication/Python/long-multiplication-1.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#!/usr/bin/env python
|
||||
print 2**64*2**64
|
||||
33
Task/Long-multiplication/Python/long-multiplication-2.py
Normal file
33
Task/Long-multiplication/Python/long-multiplication-2.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
def add_with_carry(result, addend, addendpos):
|
||||
while True:
|
||||
while len(result) < addendpos + 1:
|
||||
result.append(0)
|
||||
addend_result = str(int(addend) + int(result[addendpos]))
|
||||
addend_digits = list(addend_result)
|
||||
result[addendpos] = addend_digits.pop()
|
||||
|
||||
if not addend_digits:
|
||||
break
|
||||
addend = addend_digits.pop()
|
||||
addendpos += 1
|
||||
|
||||
def longhand_multiplication(multiplicand, multiplier):
|
||||
result = []
|
||||
for multiplicand_offset, multiplicand_digit in enumerate(reversed(multiplicand)):
|
||||
for multiplier_offset, multiplier_digit in enumerate(reversed(multiplier), start=multiplicand_offset):
|
||||
multiplication_result = str(int(multiplicand_digit) * int(multiplier_digit))
|
||||
|
||||
for addend_offset, result_digit_addend in enumerate(reversed(multiplication_result), start=multiplier_offset):
|
||||
add_with_carry(result, result_digit_addend, addend_offset)
|
||||
|
||||
result.reverse()
|
||||
|
||||
return ''.join(result)
|
||||
|
||||
if __name__ == "__main__":
|
||||
sixtyfour = "18446744073709551616"
|
||||
|
||||
onetwentyeight = longhand_multiplication(sixtyfour, sixtyfour)
|
||||
print(onetwentyeight)
|
||||
43
Task/Long-multiplication/Python/long-multiplication-3.py
Normal file
43
Task/Long-multiplication/Python/long-multiplication-3.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
'''Long multiplication'''
|
||||
|
||||
from functools import reduce
|
||||
|
||||
|
||||
def longmult(x, y):
|
||||
'''Long multiplication.'''
|
||||
return reduce(
|
||||
digitSum,
|
||||
polymul(digits(x), digits(y)), 0
|
||||
)
|
||||
|
||||
|
||||
def digitSum(a, x):
|
||||
'''Left to right decimal digit summing.'''
|
||||
return a * 10 + x
|
||||
|
||||
|
||||
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))
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def mult_table(xs, ys):
|
||||
'''Rows of all products.'''
|
||||
return [[x * y for x in xs] for y in ys]
|
||||
|
||||
|
||||
def digits(x):
|
||||
'''Digits of x as a list of integers.'''
|
||||
return [int(c) for c in str(x)]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(
|
||||
longmult(2 ** 64, 2 ** 64)
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue