This is a Non-traditional way of multiplication .But due to its better time efficiency in computer application It can use as better way of multiplication in computer program.
Note : let we have to find X*Y then Karatsuba Multiplication is applicable if and only if both X &Y have same number of digits .
Ex : x=2345 & y =4535 both have 4 digits
X and y , Both have 4 digits so Karatsuba Multiplication is applicable .
Now Fallow steps as shown in figure :
Note : let we have to find X*Y then Karatsuba Multiplication is applicable if and only if both X &Y have same number of digits .
Ex : x=2345 & y =4535 both have 4 digits
Algorithm for Karatsuba Multiplication :
Let x=5678 and y =1234X and y , Both have 4 digits so Karatsuba Multiplication is applicable .
Now Fallow steps as shown in figure :
Python Code for Karatsuba Multiplication :
- # Karatsuba Multiplication
- # Python 2.7
- # http://beginer2cs.blogspot.com
- '''
- This Karatsuba Multiplication algorithm is only applicable if number of digit in X
- is equal to number of digit of Y
- '''
- def Karatsuba_Mult(x,y):
- #convert x & y to string
- m=str(x)
- n=str(y)
- # find length of string
- lm=len(m)
- ln=len(n)
- if(lm != ln):
- return '\n This algo is only applicable if number of digit in both x and y is same :'
- #find value of a,b,c,d by slice string then convert to int
- a=int(m[0:lm/2])
- b=int(m[lm/2:])
- c=int(n[0:ln/2])
- d=int(n[ln/2:])
- # solving expression of this algorithm and return
- if(lm%2 ==0): # if length is even (why this ??? think)
- return (10**(lm))*a*c + (10**(lm/2))*(a*d +b*c) + b*d
- else: # when legth of number is odd (why this ??? think)
- return (10**(lm+1))*a*c + (10**((lm/2)+1))*(a*d +b*c) + b*d
- #################### MAIN ####################
- #taking Input
- x =int(raw_input("Enter value of X : "))
- y =int(raw_input("Enter value of Y : "))
- #Print result
- print 'X*Y using Karatsuba Multiplication :- ',Karatsuba_Mult(x,y)
Output Demo:
Hi there, its fastidious post on the topic of media print, we all be familiar with media is a wonderful source of facts.
ReplyDeleteAlso visit my web site; attorney car accidents
I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Data Science with Python , kindly contact us http://www.maxmunus.com/contact
ReplyDeleteMaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us.
Sangita Mohanty
MaxMunus
E-mail: sangita@maxmunus.com
Skype id: training_maxmunus
Ph:(0) 9738075708 / 080 - 41103383
http://www.maxmunus.com/