Three building blocks: sequence, branch, loop
Every algorithm is built from three blocks:
- Sequence: do step 1, then step 2, then step 3.
- Branching:
if condition: A else: B. Only one of A or B runs. - Loop:
while condition: do the steps. The steps run again and again while the condition is true.
A condition is a question with the answer true or false, for example n % 2 == 0. The % sign gives the remainder after division (7 % 2 is 1).
Compound conditions, maximum and minimum
Join conditions with and (both must be true), or (at least one true) and not (flip the answer). To check that x is between 10 and 20 write x >= 10 and x <= 20.
Maximum of two numbers: if a > b: max = a else: max = b. Of three numbers: start with max = a; then if b > max: max = b; then if c > max: max = c. For the minimum, flip the sign. Check: for 7, 12, 9 the max is 12 and the min is 7.
Branching in maths: the quadratic equation
For a x² + b x + c = 0 compute the discriminant D = b² - 4ac. Then branch: if D > 0 there are two real roots x = (-b ± √D) / (2a); if D = 0 there is one root -b / (2a); if D < 0 there is no real root. Example: x² - 5x + 6 = 0 has D = 25 - 24 = 1, so roots (5 ± 1)/2 = 3 and 2.
Loops on numbers: digits of a number
Two tools: n % 10 is the last digit and n // 10 (whole-number division) drops the last digit. To add the digits of n:
sum = 0; while n > 0: sum = sum + n % 10; n = n // 10
Trace for 347: last digit 7 (sum 7, n 34), then 4 (sum 11, n 3), then 3 (sum 14, n 0). Stop. The sum is 14. The same loop can count digits, reverse a number or check a palindrome.
Prime test
A number n > 1 is prime if only 1 and n divide it. Loop d from 2 up while d × d <= n; if n % d == 0, n is not prime. If the loop ends with no divisor, n is prime. Why only up to √n? If n = p × q, one of p, q is not bigger than √n. For 29, check 2, 3, 4, 5 (5 × 5 = 25 <= 29): none divides, so 29 is prime. For 21 we meet 3 and stop.
Euclid's algorithm for the GCD
The GCD (greatest common divisor) is the biggest number that divides both. Euclid's idea: the GCD does not change if you replace the bigger number by (bigger - smaller). Keep going until the numbers are equal. Example (18, 12): (6, 12) then (6, 6). GCD = 6. A faster form uses the remainder: while b != 0: a, b = b, a % b. For (48, 18): (18, 12), (12, 6), (6, 0), so the GCD is 6.
Try it
Try it: On paper, trace the digit loop for 4829 in a table with columns n, last digit, sum. Then use the 3D digit loop and press Next step to check each row. Predict first: will the sum be bigger than 20?
Key formulas and definitions
- if condition: A else: B
- while condition: steps (needs a way to stop)
- last digit = n % 10; drop last digit: n = n // 10
- D = b² − 4ac; D > 0: 2 roots, D = 0: 1 root, D < 0: none
- Prime: no divisor d with 2 ≤ d ≤ √n
- GCD(a, b) = GCD(b, a % b); stop when b = 0
Worked examples
1. Write the condition that is true only when a number x is between 10 and 20 (both included).
x >= 10 and x <= 20. Both parts must be true, so "and" is needed.
2. Find the maximum of 7, 12 and 9 with the start-and-compare method.
max = 7. 12 > 7 so max = 12. 9 > 12 is false so max stays 12. Answer: 12.
3. Find the sum of the digits of 347 with a loop.
sum 0. 347: last digit 7, sum 7, n 34. Then 4: sum 11, n 3. Then 3: sum 14, n 0. Stop. Sum = 14.
4. Is 29 prime?
Check d = 2, 3, 4, 5 (5×5 = 25 <= 29). 29 % 2 = 1, % 3 = 2, % 4 = 1, % 5 = 4. None is 0, so 29 is prime.
5. Find the GCD of 48 and 18 using remainders.
(48, 18) → (18, 12) → (12, 6) → (6, 0). GCD = 6.
6. Solve x² - 5x + 6 = 0 using the branching rule.
D = 25 - 24 = 1 > 0, so two roots: (5 + 1)/2 = 3 and (5 - 1)/2 = 2.
Common mistakes
- Writing = when you mean ==. One sets a value, the other compares.
- A loop that never stops: forgetting to change n inside the loop, so the condition stays true.
- Using "or" when you need "and" (for example "x >= 10 or x <= 20" is always true).
- Calling 1 a prime number. Primes need exactly two divisors, so 1 is not prime and 2 is the smallest prime.