Answered step by step
Verified Expert Solution
Link Copied!

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

image

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... 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

What is a property tax lien?

Answered: 1 week ago