Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def square(x,y):
- if x<10 or y<10:
- return x*y
- strx,stry = str(x),str(y)
- n = max(len(strx) , len(stry))
- # Calculate Half Length
- half_len = (n+1)//2
- # a is first half of x, b is second half of x
- a,b = int(strx[:-half_len] or '0') , int(strx[-half_len:])
- c,d = int(stry[:-half_len] or '0') , int(stry[-half_len:])
- C0 = square(b , d)
- C1 = square((a + b) , (c + d))
- C2 = square(a, c)
- return (C2*10**(2*half_len)) + ((C1-C2-C0)**10*half_len) + C0
- n = 90827103459082710345
- print("Square of 20 digit Number:",square(n,n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement