Advertisement
Eternoseeker

S-Ass1-Karatsuba

Nov 24th, 2023
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | Source Code | 0 0
  1. def divide_and_conquer(x,y):
  2.     if len(str(x)) == 1 or len(str(y)) == 1:
  3.         return x*y
  4.     else:
  5.         n = int(max(len(str(x)), len(str(y))))
  6.         nby2 = int(n / 2)
  7.        
  8.         a = x // 10**(nby2)
  9.         b = x % 10**(nby2)
  10.         c = y // 10**(nby2)
  11.         d = y % 10**(nby2)
  12.        
  13.         ac = divide_and_conquer(a,c)
  14.         bd = divide_and_conquer(b,d)
  15.         ad_plus_bc = divide_and_conquer(a+b, c+d) - ac - bd
  16.        
  17.         prod = ac * 10**(2*nby2) + (ad_plus_bc * 10**nby2) + bd
  18.  
  19.         return prod
  20.  
  21. x = int(input("Enter 20 digit num: "))
  22. ans = divide_and_conquer(x, x)
  23. print(f"Square of number {x} is {ans}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement