-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaratsuba.py
More file actions
57 lines (42 loc) · 1.89 KB
/
karatsuba.py
File metadata and controls
57 lines (42 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# encoding: utf-8
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Copyright (c) 2022 Vladimir Shurygin. All rights reserved.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ (c) 2017 Vladimir Shurygin. All rights reserved.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
https://courses.csail.mit.edu/6.006/spring11/exams/notes3-karatsuba
https://brilliant.org/wiki/karatsuba-algorithm/
"""
from math import ceil, floor
# math.ceil(x) Return the ceiling of x as a float, the smallest integer value greater than or equal to x.
# math.floor(x) Return the floor of x as a float, the largest integer value less than or equal to x.
def karatsuba(x, y):
"""
The Karatsuba algorithm decreases the number of subproblems
to three and ends up calculating the product of two -bit numbers
in time--a vast improvement over the naive algorithm.
"""
# base case
if x < 10 and y < 10: # in other words, if x and y are single digits
return x * y
n = max(len(str(x)), len(str(y)))
# Cast n into a float because n might lie outside the representable range of integers.
m = int(ceil(float(n) / 2))
x_H = int(floor(x / 10 ** m))
x_L = int(x % (10 ** m))
y_H = int(floor(y / 10 ** m))
y_L = int(y % (10 ** m))
# recursive steps
a = karatsuba(x_H, y_H)
d = karatsuba(x_L, y_L)
e = karatsuba(x_H + x_L, y_H + y_L) - a - d
return int(a * (10 ** (m * 2)) + e * (10 ** m) + d)
if __name__ == "__main__":
import argparse
PARSER = argparse.ArgumentParser('karatsuba', 'karatsuba num1 num2')
PARSER.add_argument('num1', type=int)
PARSER.add_argument('num2', type=int)
OPTS = PARSER.parse_args()
print(karatsuba(OPTS.num1, OPTS.num2))