Efficiency of algorithms

EasyMedium
123456789
Question 3
Easy

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

Algorithm A

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

Algorithm B

n = int(input("Enter n: "))
total = n * (n + 1)
print(total)

Algorithm B is more efficient than Algorithm A.

Justify this statement.

[2]

Efficiency of algorithms Questions

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