Last active
November 19, 2021 15:36
-
-
Save gunavaran/db9d3f4f3267dfb1faaeb089680a7f14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#acknowledgement: 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): | |
#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))) | |
m = ceil(n/2) #Cast n into a float because n might lie outside the representable range of integers. | |
x_H = floor(x / 10**m) | |
x_L = x % (10**m) | |
y_H = floor(y / 10**m) | |
y_L = 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment