Engineering Programming (MATLAB/Python)

Variables, arrays and control flow

Use this for “How many times does for k = 1:2:10 run, and what is the total of k?”

Chart

051015202512345term number nS = 25ℓ = 9terms aₙsums Sₙ
Terms: 1, 3, 5, 7, 9 (add s = 2 each time).
ℓ = 1 + 4 × 2 = 9
S
=
5
×
1 + 9
2
=
25
This is an example. Type your own values to start a new problem.
for
k
=
a
:
s
:
b
n
Passes
calculated
ℓ
Last term
calculated
S
Sum of terms
calculated

Formulas

n
=
⌊
b
−
a
s
⌋
+
1
5
=
⌊
10 − 1
2
⌋
+
1
ℓ
=
a
+
(
n
−
1)
×
s
9 = 1 + (5 − 1) × 2
S
=
n
×
a
+
ℓ
2
25
=
5
×
1 + 9
2

Assumptions

•
The start, step and stop are whole numbers, and the step is positive.
•
MATLAB’s a:s:b stops at b or before; Python’s range(a, b + 1, s) gives the same terms.
•
Python’s stop is left out: range(1, 10, 2) is 1, 3, 5, 7, 9.

Step-by-step

WE KNOW
a = 1, s = 2, b = 10
FIND
passes (n), last term (ℓ), sum of terms (S)
Step 1 · Find passes (n)
Use n = ⌊(b − a) ÷ s⌋ + 1
Whole
steps
of
s
fit
in
the
span
b
−
a
;
the
start
adds
one
more
pass.
n
=
⌊
10 − 1
2
⌋
+
1
⌊
9
2
⌋
=
⌊4.5⌋
=
4
4 + 1 = 5
n
=
5
Step 2 · Find last term (ℓ)
Use ℓ = a + (n − 1) × s
After
the
first
pass,
each
of
the
other
n
−
1
passes
adds
one
step.
ℓ
=
1
+
(5
−
1)
×
2
ℓ
=
1
+
4
×
2
ℓ
=
1
+
8
ℓ
=
9
Step 3 · Find sum of terms (S)
Use S = n × (a + ℓ) ÷ 2
Pair
the
first
term
with
the
last,
the
second
with
the
one
before
it:
each
pair
adds
to
a
+
ℓ
.
S
=
5
×
1 + 9
2
S
=
5
×
10
2
S
=
50
2
S
=
25
Check
Put the numbers back in. Both sides should match.
5
=
⌊
10 − 1
2
⌋
+
1
✓
9 = 1 + (5 − 1) × 2 ✓
25
=
5
×
1 + 9
2
✓
Topic check
See what you know: 5 questions on this topic
New questions each time. Pass to mark it complete.

More problem types

© 2026 SME. All rights reserved.