Can you please do this using c++? Q3 Russian peasant multiplication Russian peasant multiplication is an
Fantastic news! We've Found the answer you've been seeking!
Question:
Can you please do this using c++?
Transcribed Image Text:
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.. 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..
Expert Answer:
Answer rating: 100% (QA)
C code with output for a89b21 maincpp 1 include 2 using namesp... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Can you please do the question above the name ishita in like 6-7 pages. 1. Jaskaran: What is consumer surplus? What is Producer surplus? Graph, Example and Mathematical explanation for each. 2....
-
Please speculate: Why do you think this UK Hoover fiasco happened in the first place? What went wrong?
-
Please explain and show me how to do this on Access. Thank you! I posted this question before, and the person who answered did not answer the full question and did not answer it correctly, I have...
-
1 Which branch of our government makes our laws? A O All choices Judicial Legislative Executive
-
If D0 = $2.75, g (which is constant) = 3%, and P0 = $36, what is the stock's expected total return for the coming year? A. 9.82% B. 10.07% C. 10.33% D. 10.60% E. 10.87%
-
The bridge is constructed as a three-hinged trussed arch. Determine the horizontal and vertical components of reaction at the hinges (pins) at A, B, and C. The dashed member DE is intended to carry...
-
Spice Inc. issued bonds payable on December 31. Spice's bonds were dated July 31. Which statement is true of Spice's journal entry to record issuance of the bonds payable? a. Spice must pay one...
-
Roscoe contributes a personal use asset with an adjusted basis of $15,000 and a fair market value of $28,000 on the contribution date. Determine if any gain or loss is recognized and the basis under...
-
Room Type I (Mountain View) Type II (Street View) Rental Class Super Saver Deluxe Business $30 $35 $15 $25 $35 Round Tree's management makes a forecast of the demand by rental class for each night in...
-
In this mini-case, you will complete the test of details on accounts receivable for the 2019 audit of EarthWear Clothiers, Inc. The principal test of detail involves sending "confirmations" or...
-
Patrick Yang and Celine Smith are married with 2 children, aged 12 and 20. Celine is the vice president of ABC Ltd., a Canadian-controlled private corporation located in Mississauga, Ontario. ABC is...
-
A company produces two products (product 1 and product 2) on two machines (machine 1 and machine 2). Amount of machine time and labor time which depends on the machine and the product, and the cost...
-
Black and Shannon have decided to form a partnership. They have agreed that Black is to invest $360,000 and that Shannon is to invest $120,000. Black is to devote one-half time to the business, and...
-
Stats Air operates a daily flight between Sydney and Griffith that has operated for six years. Flight XZ 966 is scheduled to depart Sydney at 11:35 and arrive in Griffith at 13:10. Flight XZ 967 is...
-
For investment advisors, a major consideration in planning for a client in retirement is the determination of a withdrawal amount that will provide the client with the funds necessary to maintain his...
-
1) The nominal interest rate on a ten-year bond is 10%. The expected average annual inflation rate over the ten years is 3%, while additional risks account for a premium of 5% (default risk, maturity...
-
In this energy balance system: what is the stock, and what are the inflows and outflows? Is the Earth system as a whole (Earth's surface + the atmosphere; the red boxes on your worksheet) in...
-
Would you use the adjacency matrix structure or the adjacency list structure in each of the following cases? Justify your choice. a. The graph has 10,000 vertices and 20,000 edges, and it is...
-
Calculate the amount of the child and dependent care credit allowed for 2012 in each of the following cases, assuming the taxpayers had no income other than the stated amounts. a. William and Carla...
-
Rebecca and Walter Bunge have been married for 5 years. They live at 883 Scrub Brush Street, Apt. 52B, Las Vegas, NV 89125. Rebecca is a homemaker and Walt is a high school teacher. Rebecca's Social...
-
Phil and Linda are 25-year-old newlyweds and file a joint tax return. Linda is covered by a retirement plan at work, but Phil is not. a. Assuming Phil's wages were $27,000 and Linda's wages were...
-
On April 1, Caesar Salad Dressings creates a petty cash fund with an imprest balance of \(\$ 400\). During April, Elise Nelson, the fund custodian, signs the following petty cash tickets: On April...
-
Pendley Productions makes all sales on credit. Cash receipts arrive by mail. Larry Padgitt in the mailroom opens envelopes and separates the checks from the accompanying remittance advices. Padgitt...
-
Suppose that on June 1, Cool Gyrations, a disc jockey service, creates a petty cash fund with an imprest balance of \(\$ 300\). During June, Carol McColgin, fund custodian, signs the following petty...
Study smarter with the SolutionInn App