Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you please do this using c++? Q3 Russian peasant multiplication Russian peasant multiplication is an algorithm to multiply two (positive) integers. It is actually
Can you please do this using c++?
Q3 Russian peasant multiplication Russian peasant multiplication is an algorithm to multiply two (positive) integers. It is actually an old algorithm. There is evidence it was known by the ancient Egyptians. . It is simplest to explain with an example. Suppose we wish to multiply 89 x 21. 1. Let a 89 and b= 21. Form a table of three columns of numbers as follows. a 89 44 22 5 2 1 b 21 42 84 168 336 672 1344 21 168 336 1344 1869 (sum) 2. At each step, if a is odd, we copy the value of b into the third column. 3. Then we divide a by 2 (integer division) and multiply b by 2. 4. We stop when the value of a reaches 0. 5. The value of a x b is the sum of the numbers in the third column. Hence the algorithm breaks down the multiplication of two (possibly large) numbers into a set of additions and integer multiplications and divisions by 2. 1. Integer multiplication and division by 2 are easy operations in binary. 2. Integer multiplication by 2 is a left shift of the binary digits of a number. 3. Integer division by 2 is a right shift of the binary digits of a number (and loss of the "least significant bit"). 4. Addition is also a simpler operation than multiplication, in general. Implement a function for Russian peasant multiplication of two positive integers. int RPM(int a, int b); 1. Declare and initialize a temporary variable int sum 0. 2. Begin a loop. (a) If a is odd then increment sum = sum + b. (b) Perform integer division a = a/2 and integer multiplication b = b*2. (c) Repeat the loop. Stop when a = 0). 3. Return the value of sum. Set a and b to the first and last four digits of your student id. If id 23054611, then a=2305 and b= 1611. Use your function to multiply a x b..
Step by Step Solution
★★★★★
3.48 Rating (164 Votes )
There are 3 Steps involved in it
Step: 1
C code with output for a89b21 maincpp 1 include 2 using namesp...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