Skip to content

Course home

Efficiency of algorithms

Efficiency of algorithms

EasyMedium
123456
Question 5

Two algorithms, Algorithm Alpha and Algorithm Beta, are designed to compute the sum of the cubes of the first n n\,n positive integers, ∑i=1ni3\sum_{i=1}^{n} i^3∑i=1n​i3.

Algorithm Alpha

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

Algorithm Beta

n = int(input("Enter a positive integer: "))
total = (n * (n + 1) // 2) ** 2
print(total)

Which statement correctly compares the efficiency of the two algorithms?

A

Both algorithms have a time complexity of O(n3)O(n^3)O(n3) because they calculate the sum of cubes, meaning they are equally efficient.

B

Algorithm Alpha has a time complexity of O(n)O(n)O(n) due to the loop, while Algorithm Beta has a time complexity of O(1)O(1)O(1), making Algorithm Beta more efficient for large values of nnn.

C

Algorithm Alpha has a time complexity of O(1)O(1)O(1) as it runs in a single execution block, while Algorithm Beta has a time complexity of O(n)O(n)O(n) due to the multi-step algebraic operations, making Algorithm Alpha more efficient.

D

Both algorithms have a time complexity of O(1)O(1)O(1) because modern processors can compute both the loop and the algebraic formula in a negligible amount of time.

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