Answer question 1. There are two algorithms given in pseudo code.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Answer question 1.

There are two algorithms given in pseudo code.

 

SP(x,n) // SP stands for "slow power"
FP(x,n) // FP stands for "fast power"
These algorithms both take two parameters: a non-zero real number called x, and a natural number called
n. The algorithms do not perform any error checking of the values passed to them. They are simply
assumed to be correct. These algorithms both return a real number which is xn
Here is pseudocode for the algorithms:
SP(x,n)
FP(x,n)
result = 1
if n==0
return 1
return FP(x*x, n/2)
return x*FP(x*x,(n-1)/2)
for (i=1; i<=n; i++) result = result *x
if n is even
return result
else
In this analysis you will be comparing the efficiency of the two alorithms by counting the use of the most
expensive operation in the algorithms, which is multiplication. (Note that because all the divisions in FP
are integer divisions by 2, they can be implemented efficiently as right binary shifts).
In the questions that follow, it doesn't matter what x is.
a) How many multiplications will be used in total to calculate SP(x,n)?
b) Trace the execution of the return statements of FP(x,53), printing them statements one by
one as they are being called and stating next to it the number of multiplications in that statement.
The first line of the answer is:
return x*FP(x*x,26)
// 2 multiplications (in red: the first one, in the first parameter, is
executed before FP is called. The second one is executed after)
c) Add one line to the FP algorithm to reduce the total number of multiplications in FP(x,n)
when FP is initially called withn> 0. Rewrite the algorithm underneath:
d) What is the total number of multiplications used by your new algorithm to calculate
FP(x,53)?
Transcribed Image Text:SP(x,n) // SP stands for "slow power" FP(x,n) // FP stands for "fast power" These algorithms both take two parameters: a non-zero real number called x, and a natural number called n. The algorithms do not perform any error checking of the values passed to them. They are simply assumed to be correct. These algorithms both return a real number which is xn Here is pseudocode for the algorithms: SP(x,n) FP(x,n) result = 1 if n==0 return 1 return FP(x*x, n/2) return x*FP(x*x,(n-1)/2) for (i=1; i<=n; i++) result = result *x if n is even return result else In this analysis you will be comparing the efficiency of the two alorithms by counting the use of the most expensive operation in the algorithms, which is multiplication. (Note that because all the divisions in FP are integer divisions by 2, they can be implemented efficiently as right binary shifts). In the questions that follow, it doesn't matter what x is. a) How many multiplications will be used in total to calculate SP(x,n)? b) Trace the execution of the return statements of FP(x,53), printing them statements one by one as they are being called and stating next to it the number of multiplications in that statement. The first line of the answer is: return x*FP(x*x,26) // 2 multiplications (in red: the first one, in the first parameter, is executed before FP is called. The second one is executed after) c) Add one line to the FP algorithm to reduce the total number of multiplications in FP(x,n) when FP is initially called withn> 0. Rewrite the algorithm underneath: d) What is the total number of multiplications used by your new algorithm to calculate FP(x,53)?
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY