Count the number of multiplications in terms of n in the sec

Count the number of multiplications, in terms of n, in the section of code shown below:

MATlab

for   k =1 : 1 : n

        m = 3 * k + 1;

        for j =1 :1 : m

             a = b * c + 4.1;

        end

        b = a * c ;

end

Solution

for   k =1 : 1 : n
        % 1st multiplication
        m = 3 * k + 1;
        for j =1 :1 : m
        % 2nd multiplication
            a = b * c + 4.1;
        end
        % 3rd multiplication
        b = a * c ;
end

%{
Every time the first for loop runs, 1st and 3rd multiplication
takes place. Thus, 1st multiplication occurs n times and 3rd multiplication
also occurs n times.
Now, the multiplication inside nested for loop i.e 2nd muliplication
occurs m times and m is 3*k + 1, and k goes from 1 to n.
count of 2nd multiplication = summation (1 to n) (3*k + 1)
count of 2nd multiplication = 3*n*(n+1)/2 + n
count of 1st multiplication = n
count of 3rd multiplication = n
Total mulltiplication: 2n + 3*n*(n+1)/2 + n
=> 2n + (3*n2)/2 + (3*n)/2 + n
=> (3*n2)/2 + (9*n)/2

%}

Count the number of multiplications, in terms of n, in the section of code shown below: MATlab for k =1 : 1 : n m = 3 * k + 1; for j =1 :1 : m a = b * c + 4.1;

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site