Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

Final contract price to be paid is certain in cost plus contracts.

Answered: 1 week ago