A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 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)
|
||||
20
Task/Long-multiplication/Python/long-multiplication-3.py
Normal file
20
Task/Long-multiplication/Python/long-multiplication-3.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
def digits(x):
|
||||
return [int(c) for c in str(x)]
|
||||
|
||||
def mult_table(xs, ys):
|
||||
return [[x * y for x in xs] for y in ys]
|
||||
|
||||
def polymul(xs, ys):
|
||||
return map(lambda *vs: sum(filter(None, vs)),
|
||||
*[[0] * i + zs for i, zs in enumerate(mult_table(xs, ys))])
|
||||
|
||||
def longmult(x, y):
|
||||
result = 0
|
||||
for v in polymul(digits(x), digits(y)):
|
||||
result = result * 10 + v
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
print longmult(2**64, 2**64)
|
||||
Loading…
Add table
Add a link
Reference in a new issue