1. Sums of sequences
VC2M7N10 level 7 Use mathematical modelling to solve practical problems involving rational numbers and percentages, including financial contexts such as ‘best buys’; formulate problems, choosing representations and efficient calculation strategies, designing algorithms and using digital tools as appropriate; interpret and communicate solutions in terms of the situation, justifying choices made about the representation.
finding the sum of a set of consecutive numbers using a loop structure
1.1. Sum of consecutive numbers
1.1.1. Listed numbers
The code below uses a for loop which iterates over the nums list.
Each number, num, in the list, nums, is added to the sum.
nums = [1, 2, 3, 4, 5]
sum = 0
for num in nums:
sum += num
print(sum)
The pseudocode below uses a for each loop to iterate over the nums list.
BEGIN
nums ← [1, 2, 3, 4, 5]
sum ← 0
FOR EACH num IN nums DO
sum ← sum + num
ENDFOR
PRINT "Sum is ", sum
END
1.1.2. Range from 0
The code below uses the range function to provide a list of integers from 0 to 5.
nums = list(range(6))
print(nums)
sum = 0
for num in nums:
sum += num
print(sum)
The pseudocode below uses a for each loop to iterate over the nums list.
BEGIN
nums ← [0, 1, 2, 3, 4, 5]
PRINT "Numbers are ", nums
sum ← 0
FOR EACH num IN nums DO
sum ← sum + num
ENDFOR
PRINT "Sum is ", sum
END
1.1.3. Range: first and last
The code below uses the range function to provide a list of integers from start_num to end_num.
start_num = 4
end_num = 12
nums = list(range(start_num, end_num + 1))
print(nums)
sum = 0
for num in nums:
sum += num
print(sum)
The pseudocode below uses a for each loop to iterate over the nums list.
BEGIN
start_num ← 4
end_num ← 12
nums ← LIST OF INTEGERS FROM start_num TO end_num
PRINT "Numbers are ", nums
sum ← 0
FOR EACH num IN nums DO
sum ← sum + num
ENDFOR
PRINT "Sum is ", sum
END
1.1.4. Range: step size
The code below uses the range function to provide a list of integers from start_num to end_num in steps of step_size.
start_num = 4
end_num = 12
step_size = 2
nums = list(range(start_num, end_num + 1, step_size))
print(nums)
sum = 0
for num in nums:
sum += num
print(sum)
The pseudocode below uses a for each loop to iterate over the nums list.
BEGIN
start_num ← 4
end_num ← 12
step_size ← 2
nums ← LIST OF INTEGERS FROM start_num TO end_num IN STEPS OF step_size
PRINT "Numbers are ", nums
sum ← 0
FOR EACH num IN nums DO
sum ← sum + num
ENDFOR
PRINT "Sum is ", sum
END
1.1.5. Arithmetic sequence formula 1
One formula for the sum of a sequence of numbers with the same difference between them is:
S = n/2[2a + (n-1)d]
where
S is the sum
n is the number of numbers
a is the start number
d is the difference between numbers
a = 4
n = 5
d = 2
sum = (n/2) * (2*a + (n-1)*d)
print(sum)
The pseudocode below calculates the sum using this formula.
BEGIN
a ← 4 -- first term
n ← 5 -- number of terms
d ← 2 -- common difference
sum ← (n ÷ 2) * (2 * a + (n - 1) * d)
PRINT "Sum of arithmetic progression is ", sum
END
1.1.6. Arithmetic sequence formula 2
Another formula for the sum of a sequence of numbers with the same difference between them is:
S = n/2[a + l]
where
S is the sum
n is the number of numbers
a is the start number
l is the last number
a = 4
n = 5
l = 12
sum = (n/2) * (a + l)
print(sum)
The pseudocode below calculates the sum using this formula.
BEGIN
a ← 4 -- first term
n ← 5 -- number of terms
l ← 12 -- last term
sum ← (n ÷ 2) * (a + l)
PRINT "Sum of arithmetic progression is ", sum
END