Compute the actual number of additions subtractions multipli
Solution
Compute the actual number of additions, subtractions, multiplications, divisions, and
comparisons that must be performed with each of these algorithms:
a. for i = 3 to n-1 //This loop runs for values 3 to n-1, i.e., a total of n-1-3+1 = n-3 times.
a = 3 * n + 2 * i - 1; //This equation involves, 2 multiplications, 1 addition, and 1 subtraction.
Therefore, the number of multiplications required is 2 * (n-3),
the number of additions required is (n-3),
the number of subtractions required is (n-3).
b. for i = 1 to floor(n/2) //This loop runs for values 1 to floor(n/2).
a = n - i; //This equation involves only a subtraction.
Therefore, the number of subtractions required is floor(n/2).
c. t = 0; //This is an assignment, and will be done only once.
for i = 1 to n //This loop runs for values 1 to n, i.e., a total of n times.
{ //Any statement inside this block will be executed n times.
s = 0; //This is an assignment. So, total n assignments.
for j = 1 to i //This loop runs for values 1 to i, i.e., a total of i times.
s = s + a[j] //This statements involves a single addition, and therefore, will be executed i times where i values runs from 1 .. n.
t = t + s^2 //This statement involves an addition, and an exponentiation.
}
Therefore, the number of additions involved in this block is: 1 + 2 + 3 + ... n i.e., n(n+1)/2 + n.
