Skip to content

Course home

Efficiency of algorithms

Efficiency of algorithms

EasyMedium
123456
Question 2

Two programmers, Ada and Charles, write different Python programs to calculate the sum of the squares of the first n n\,n positive integers: ∑i=1ni2\sum_{i=1}^{n} i^2∑i=1n​i2.

Program Ada

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

Program Charles

n = int(input())
total = (n * (n + 1) * (2 * n + 1)) // 6
print(total)

Which of the following statements correctly describes and compares the worst-case time complexities of these programs?

A

Both programs have a worst-case time complexity of O(n)O(n)O(n), meaning they are equally efficient as nnn grows large.

B

Program Ada has a worst-case time complexity of O(n2)O(n^2)O(n2) due to the exponentiation i ** 2, while Program Charles has a worst-case time complexity of O(1)O(1)O(1), making Program Charles more efficient.

C

Program Ada has a worst-case time complexity of O(n)O(n)O(n) and Program Charles has a worst-case time complexity of O(1)O(1)O(1), making Program Charles more efficient for large values of nnn.

D

Program Ada has a worst-case time complexity of O(1)O(1)O(1) because the loop bounds are known, while Program Charles has a worst-case time complexity of O(n)O(n)O(n) due to the multi-variable multiplication.

Markscheme

Efficiency of algorithms Questions

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

15 exam-style questions on AQA GCSE Computer Science Efficiency of algorithms. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank