Question

In: Computer Science

in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based...

in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based on the fact that xy = 2(x · (y/2)) when y is even and xy = 2(x · ⌊y/2⌋) + x when y is odd, together with the initial condition xy = 0 when y = 0.

Solutions

Expert Solution

// C++ Program to find Product
// of 2 Numbers using Recursion
#include <bits/stdc++.h>
#include <math.h>
using namespace std;

// recursive function to calculate
// multiplication of two numbers
int product(int x, int y)
{    if(y==0) //return ) if y is 0 intial condition
       return 0;
   // if y is even
   else if (y%2==0)
       return (2*product(x, y/2));

   // if y is odd
   else
       return (x + 2*product(x, floor((y - 1)/2)));

   // if any of the two numbers is
   // zero return zero
   }

// Driver Code
int main()
{
   int x = 23, y = 3;
   cout << product(x, y);
   return 0;
}
Output :

69

Thumbs up will be appreciated


Related Solutions

a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based on the formula 3n = 3n−1 + 3n−1 + 3n−1 b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem?
Implement and complement the recursive code to compute the product of two positive integers, x and...
Implement and complement the recursive code to compute the product of two positive integers, x and y, using only addition and/or subtraction. The function will take as its arguments two integers to multiply together ( x x y ) and will return the product. Hint: consider the following: 6 x 1 = 6 6 x 2 = 6 + (6 x 1) 6 x 3 = 6 + (6 x 2) = 6 + [6 + (6 x 1)] =...
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an...
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an m-bit number and y is an n-bit number (10 points), and analyze the time complexity of this algorithm (10 points).
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers....
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers. If x=2, y=4, the method pow should return 16. Java answers only please.
2. Give a recursive algorithm to compute the product of two positive integers, m and n,...
2. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. Java Language...
Now we need to develop a recursive algorithm for multiplication operation with two positive integers without...
Now we need to develop a recursive algorithm for multiplication operation with two positive integers without using the multiplication operator. Your answer should be in pseudocode or Java code format. Then use the Three-Question Approach to verify your recursive algorithm. No coding is required for this assignment 1. The Base-Case Question Is   there   a   nonrecursive   way   out   of   the   algorithm,   and   does   the   algorithm   work   correctly   for   this   base   case? 2. The Smaller-Caller Question Does   each   recursive   call   to   the  ...
Using Euclidean algorithm, Find integers x and y with 65537x + 3511y = 17.
Using Euclidean algorithm, Find integers x and y with 65537x + 3511y = 17.
Use Euclid’s algorithm to find integers x, y and d for which 3936 x + 1293...
Use Euclid’s algorithm to find integers x, y and d for which 3936 x + 1293 y = d is the smallest possible positive integer. Using your answers to this as your starting point, do the following tasks. (a) Find a solution of 3936 x ≡ d mod 1293. (b) Find an integer r that has the property that r ≡ d mod 1293 and r ≡ 0 mod 3936. (c) Find an integer R that has the property that...
Describe an optimal algorithm for finding the integers common to two lists of n integers each....
Describe an optimal algorithm for finding the integers common to two lists of n integers each. Evaluate how long each step in your algorithm takes using Θ-notation.
Use Euclid’s algorithm to find integers x, y and d for which 3936x + 1293y =...
Use Euclid’s algorithm to find integers x, y and d for which 3936x + 1293y = d is the smallest possible positive integer. Using your answers to this as your starting point, do the following tasks. (a)Find an integer s that has the property that s ≡ d mod 3936 and s ≡ 0 mod 1293. (b) Find an integer S that has the property that S ≡ 573 mod 3936 and S ≡ 0 mod 1293. (c) Find an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT