Efficiency of algorithms

EasyMedium
123456789
Question 8
Easy

Two different programs are written to calculate the sum of the first n n\,n odd positive integers.

Program A

n = int(input("Enter a positive integer: "))
total = 0
for i in range(1, n + 1):
    total = total + (2 * i - 1)
print(total)

Program B

n = int(input("Enter a positive integer: "))
total = n * n
print(total)

Which of the following statements is true about the efficiency of these programs?

Both programs are equally efficient.

Program A is more efficient than Program B.

Program B is more efficient than Program A.

The relative efficiency of the two programs cannot be determined without knowing the value of nnn.

Efficiency of algorithms Questions

  1. GCSE
  2. /Computer Science
  3. /Efficiency of algorithms