Question #45616

How many elementary operations are used in the following algorithm?

Step 1 Set a =1, b = 1, c = 2, k = 1

Step 2 While k < n

(a)replace c with a+b

(b)replace a with b

(c)replace b with c

(d)replace k with k + 1

endwhile

Step 3 Print b

Step 1 Set a =1, b = 1, c = 2, k = 1

Step 2 While k < n

(a)replace c with a+b

(b)replace a with b

(c)replace b with c

(d)replace k with k + 1

endwhile

Step 3 Print b

Expert's answer

Step 1: There are 3 operations (3 "=").

Step 2: There are 6(n-1)+ n=7n-6 operations. (n-1) times 6 (3 "=" and 3 "+") and n checkings if k<n).

Step 3: print() is not elementary operation.

Thus we have 3+(7n-6)+0=7n-3 operations.

Answer: 7n-3.

Step 2: There are 6(n-1)+ n=7n-6 operations. (n-1) times 6 (3 "=" and 3 "+") and n checkings if k<n).

Step 3: print() is not elementary operation.

Thus we have 3+(7n-6)+0=7n-3 operations.

Answer: 7n-3.

## Comments

## Leave a comment