Question
This is the C Code for Karatsuba multiplication. Could you explain this line by line? C Code: #include #include int max(int num1, int num2) {
This is the C Code for Karatsuba multiplication. Could you explain this line by line?
C Code:
#include
#include
int max(int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
}
int digits(int x)
{
int count = 0;
while (x != 0)
{
x /= 10;
++count;
}
return count;
}
long int karatsuba(int x,int y)
{
int n,a,b,c,d,i1,i2,half;
long int ac,bd,ad_plus_bc,fin;
if(x
{
return x*y;
}
else
{
n = max(digits(x),digits(y));
half = floor(n / 2) ;
i1 = (pow(10,half));
i2 = (pow(10,(2*half)));
a = floor(x / i1);
b = (x % i1);
c = floor(y / i1);
d = (y % i1);
ac = karatsuba(a,c);
bd = karatsuba(b,d);
ad_plus_bc = karatsuba(a+b,c+d)-ac-bd;
fin = ac * i2 + (ad_plus_bc * i1) + bd;
return fin;
}
}
int main()
{
int x,y;
long int i;
printf(\\\"Enter the Multiplicand : \\\");
scanf(\\\"%d\\\",&x);
printf(\\\"Enter the Multiplier : \\\");
scanf(\\\"%d\\\",&y);
i = karatsuba(x,y);
printf(\\\"Product is %ld \\\",i);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started