Consider the following algorithm ALGORITHM MysterynInput A n

Consider the following algorithm. ALGORITHM Mystery(n)//Input: A nonnegative integer n S leftarrow 0 for i leftarrow 1 to n do S leftarrow S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try to prove that, in fact, it cannot be done.

Solution

(a) This algorithm will compute sum of all perfect square which is less than or equal to n.

(b) addition and multiplication is basic operation.

(c) n times

(d) The given algorithm is a linear type algorithm that is O(n). Time complexity is O(n).

(e) We cannot improve this algorithm anymore. Because we atleast needs multiplication and addition n times.

 Consider the following algorithm. ALGORITHM Mystery(n)//Input: A nonnegative integer n S leftarrow 0 for i leftarrow 1 to n do S leftarrow S + i * i return S a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site