AQA GCSE Computer Science June 2022 Paper 1, Question 4: comparing the efficiency of two programs

AQA 8525 June 2022 Paper 1, Question 4: two Python programs add the integers from 1 to n, one with a loop and one with a formula. Which is more efficient and how to justify it for both marks, with a program that counts the steps.

Past paper questionAQA 8525/1BJune 2022 Paper 13 marksExplain

Question 4 of the AQA GCSE Computer Science June 2022 Paper 1 (8525/1B, the Python paper) shows two Python programs that give the same answer and asks which is more efficient (1 mark) and why (2 marks). The specification says you must be able to compare the efficiency of algorithms, and this is what that looks like in the exam.

We do not copy the exam paper here. Open it beside this page: AQA June 2022 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.

The question in short

Both programs read a whole number and output the total of all the integers from 1 up to it. Enter 5 and both output 15.

  • Program A uses a for loop. It starts a total at 0 and adds each number from 1 to num in turn.
  • Program B has no loop. It multiplies the number by one more than itself, then does an integer division by 2.

Program B is using a formula: the sum of 1 to n is n × (n + 1) ÷ 2. For 5 that is 5 × 6 ÷ 2 = 15.

The answers

Part 1. Program B is more efficient than Program A.

Part 2. Two marks need a point and the reason behind it. Either of these pairs gets both:

  • Program B takes less time to run, because fewer lines of code are executed.
  • Program B always does the same number of calculations, but in Program A the number of calculations grows as the number entered gets bigger.

The mark scheme also accepts that B uses fewer variables, so it would use less memory.

How to think about it

Efficiency at GCSE means time: how many steps the computer has to carry out. Count them.

Program B does three calculations whatever you type. For 5, three. For a million, three.

Program A goes round its loop once for every number. For 5, five additions. For a million, a million additions.

Where the marks are lost

  • "B is shorter." Both programs are six lines long. Efficiency is about lines executed, not lines written. A loop of two lines can run a million times.
  • One point with no reason. "B is faster" is one mark. "B is faster because it does not repeat" is two.
  • Choosing "equally efficient" because the outputs match. Same answer does not mean same work.

Run it

This program runs both methods and counts the additions that Program A needs. Change NUM and watch one count grow while the other stays still. The robot drives 1 cm for every 100 steps Program A took, so you can see the cost.

NUM = 5: both give 15, and A needs 5 steps. Try 1000: both give 500500, A needs 1000 steps and B still needs 3.
The program
from bugbot import *
connect()

# change NUM and press Run
NUM = 5

# Program A: a loop
total = 0
steps_a = 0
for i in range(1, NUM + 1):
    total = total + i
    steps_a = steps_a + 1
print("Program A:", total, "in", steps_a, "steps")

# Program B: a formula
num2 = NUM + 1
num2 = NUM * num2
num2 = num2 // 2
print("Program B:", num2, "in 3 steps")

if steps_a >= 100:
    forward(60, distance=min(steps_a // 100, 60))
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to AQA GCSE Computer Science 2022 Paper 1 Question 4?

Program B is more efficient. It always performs the same small number of calculations, while Program A repeats its loop once for every number up to the one entered, so it takes longer as the input gets bigger.

What does efficiency mean in GCSE Computer Science?

For AQA it means time efficiency: how many steps an algorithm takes to finish. An algorithm that needs fewer steps for the same input is more efficient.

What is the formula for adding the numbers from 1 to n?

n multiplied by n + 1, divided by 2. For n = 100 it is 100 × 101 ÷ 2 = 5050.

More from this paper

Every AQA 8525 question we have worked · Guide: Big O notation explained

Learn it step by step

  1. F5.1 What an algorithm is Algorithms
  2. F5.9 Merge sort and comparing algorithms Algorithms
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.