Introduction: an answer is not yet a procedure
We often associate computational thinking with programming. In fact, it is about tackling a problem, analysing it and building a sequence of precise instructions to solve it. We use our eyes, experience and implicit knowledge; an executable procedure must instead make the operations explicit.
There is not necessarily just one method: we can often choose between different data representations and algorithms. Here we start with seemingly obvious operations on digits, turn them into arithmetic procedures and then apply the same way of thinking to practical problems. No knowledge of a programming language is required.
Before we start: data, DIV, MOD and assignments
Unless stated otherwise, we work with non-negative integers written in base ten. A number and the text representing it are not the same thing: as a number, 05 equals 5; as text, ‘05’ retains two characters. If leading zeros matter, we must say so before choosing an algorithm.
DIV means the integer quotient and MOD the remainder. Dividing 1234 by 100 gives quotient 12 and remainder 34: these are not two rules to memorise without understanding, but two pieces of information from the same division.
1234 = 12 × 100 + 34
0 ≤ 34 < 100
In instruction blocks, = means ‘assign to the variable the value calculated on the right’. Thus S = S + C updates S: it is not an equation to solve. Lines run from top to bottom and indentation shows which instructions repeat. In conditions, however, = compares two values rather than changing them.
These are arithmetic procedures: we are not asking the machine to recognise a photograph of the digits. Strings or other methods could also be used; what matters is stating the data and the available operations.
Part one — Seven fundamental examples
The tables compare an intuitive, often visual solution with an explicit executable procedure. These are not exclusive abilities: a person can also execute the algorithm, and the same problem can have several valid algorithms.
1. Separating the last two digits
Problem. Split 1234 into the part before the last two digits and the last two digits themselves.
| Human solution | Machine solution |
|---|---|
Look at the written number and imagine a cut before its last two digits: 12 | 34. | Calculate the integer quotient when dividing by 100 to obtain A, and the remainder to obtain B. Visual recognition of the digits is not required. |
In general: A = N DIV 100; B = N MOD 100. With 987654 we obtain 9876 and 54. With 1205 we obtain 12 and 5: to display 12 | 05, B must be formatted with two digits or handled as text. If N is less than 100, A is 0.
2. Swapping two variables
Problem. Swap A = 5 and B = 8 to obtain A = 8 and B = 5.
| Human solution | Machine solution |
|---|---|
Imagine two cards showing 5 and 8 and exchange their positions. Both values remain available. | Assignments happen in sequence: A = B alone would erase the old A. First save A in the temporary variable T, then perform the swap. |
After T = A we have T = 5, A = 5, B = 8. After A = B we have T = 5, A = 8, B = 8. Finally B = T produces A = 8, B = 5. The temporary variable preserves the necessary information: an algorithm must track the state of memory throughout the steps.
3. Finding the largest of three numbers
Problem. Find the largest of 17, 42 and 29.
| Human solution | Machine solution |
|---|---|
Compare the three values mentally and recognise 42 as the largest. | Store the first value in M. Compare the others with M one at a time, updating it only when a larger value is found. |
The first comparison updates M to 42; the second leaves it unchanged because 29 is not greater than 42. We do not need separate instructions for every possible ordering. For a non-empty list, initialise M with the first element and compare each remaining element in turn.
4. Counting digits
Problem. Determine how many digits 72845 has.
| Human solution | Machine solution |
|---|---|
Read the written number and count five positions: 7, 2, 8, 4, 5. | Repeatedly divide N by 10 using integer division and count the divisions needed to reach zero. |
There are five divisions. However, we must allow for zero having one digit: a loop that only starts when N > 0 would count none on its own. For negative numbers we can first take the absolute value, without counting the minus sign as a digit.
5. Reversing the digits
Problem. Turn 1234 into 4321 without treating the number as a string.
| Human solution | Machine solution |
|---|---|
Read the digits from right to left: 4, 3, 2, 1, obtaining 4321. | Extract the last digit C with MOD 10, append it to R after multiplying R by 10, and remove that digit from N with DIV 10. Repeat until N is zero. |
| Step | Extracted digit | R | N after DIV 10 |
|---|---|---|---|
| 1 | 4 | 4 | 123 |
| 2 | 3 | 43 | 12 |
| 3 | 2 | 432 | 1 |
| 4 | 1 | 4321 | 0 |
The successive values of R are 4, 43, 432, 4321. Each integer division shortens N by one digit, which also tells us why the loop terminates. Trailing zeros in the original are not visible in the reversed number: 120 becomes 21, not a three-digit representation.
6. Recognising a palindrome
Problem. Check whether 12321 is a palindrome.
| Human solution | Machine solution |
|---|---|
Read from the left and from the right and notice the same sequence: 12321. | Save the original, calculate R with the reversal algorithm and compare the two values. They match: the number is a palindrome. |
12321 matches its reverse: it is a palindrome. 1234 and 4321 differ: 1234 is not one. The number 0 is a palindrome. This demonstrates reuse: an existing algorithm solves a different problem. Saving the original is essential because the reversal loop consumes N.
7. The principles of computational thinking
The examples reveal six tools that work together, not a rigid sequence to follow mechanically.
- Decomposition: divide a problem into parts; to recognise a palindrome, separate reversal from comparison.
- Abstraction: retain the relevant features; finding a maximum depends on the values, not the colour in which they are written.
- Pattern recognition: notice that MOD 10 isolates a digit and DIV 10 moves on to the next one.
- Generalisation: turn the calculation on 1234 into a method valid for N.
- Algorithm design: specify instructions, conditions, repetitions and termination.
- Verification: try normal and boundary cases, also looking for situations that might disprove the method.
Computational thinking does not simply mean ‘thinking like a machine’: it means formulating solutions that can be executed systematically, explained, checked and reused.
Part two — Ten problems solved with our eyes and with mathematics
In the next ten examples we mainly use non-negative integers. The aim is to obtain through explicit operations what we recognise at a glance.
8. Extracting the last digit
Problem. Extract the last digit of 5837.
| Human solution | Machine solution |
|---|---|
Look at the rightmost position and read 7. | Calculate the remainder when dividing by 10: that remainder is the last digit. |
In general, ultima = N MOD 10. The remainder is always a digit between 0 and 9. With N = 0 the result is also 0.
9. Removing the last digit
Problem. Remove the last digit of 5837.
| Human solution | Machine solution |
|---|---|
Imagine covering the final 7 and read 583. | Calculate the integer quotient when dividing by 10, discarding the remainder. |
Repeating the operation removes more digits. A single-digit number becomes 0.
10. Extracting the last three digits
Problem. Extract the last three digits of 123456.
| Human solution | Machine solution |
|---|---|
Visually isolate the last three positions: 456. | Calculate the remainder when dividing by 1000. It contains the value of the last three digits. |
The same idea applies to the last k digits using MOD 10^k, with k a positive integer. If we require exactly three positions, a remainder of 7 must be displayed as 007: the numerical value and its formatting remain two separate problems.
11. Separating a number into groups of three digits
Problem. Split 1234567 into groups of three digits.
| Human solution | Machine solution |
|---|---|
Start from the right and group the digits: 1 | 234 | 567. | Extract a group with MOD 1000 and continue with the quotient from DIV 1000. Store the groups and display them in the reverse order to extraction. |
We obtain 567 first, then 234, then 1: to display the original number, read the groups in the opposite order to extraction. Repeat until the quotient is zero; the number 0 requires a single group 0. Each group after the first in the final representation must contain three digits: 1002003 becomes 1 | 002 | 003.
12. Determining whether a number is even
Problem. Determine whether 2468 is even.
| Human solution | Machine solution |
|---|---|
Recognise 8 as an even final digit and conclude that the number is even. | Check whether dividing the number by 2 leaves remainder zero. |
If the remainder is zero, the number is even; otherwise it is odd. Zero is also even, because dividing it by 2 leaves remainder zero.
13. Adding the digits
Problem. Calculate the sum of the digits of 472.
| Human solution | Machine solution |
|---|---|
Read the digits and add 4 + 7 + 2 = 13. | Start with S = 0; extract each last digit with MOD 10, add it to S and continue with DIV 10. |
The digits arrive in the order 2, 7, 4; S becomes 2, then 9, then 13. Their order does not change the sum. With N = 0, S correctly remains 0.
14. Finding the tens digit
Problem. Find the tens digit of 5837.
| Human solution | Machine solution |
|---|---|
Identify the second position from the right and read 3. | Remove the units with DIV 10, then isolate the final remaining digit with MOD 10. |
The first step gives 583 and the second 3. If N has only one digit, its tens digit is 0.
15. Comparing the first digits of two numbers
Problem. Check whether 7824 and 7910 begin with the same digit.
| Human solution | Machine solution |
|---|---|
Look at the first position of each: read 7 and 7, so they match. | Divide each number by 10 using integer division until one digit remains, then compare the results. |
7824 → 782 → 78 → 7; 7910 → 791 → 79 → 7. The first digits match. The method also works for numbers of different lengths: 7824 and 73 both start with 7. If the input is 0, the first digit is 0 and no divisions are needed.
16. Counting occurrences of a digit
Problem. Count how often the digit 7 occurs in 727272.
| Human solution | Machine solution |
|---|---|
Scan the written number and mark each 7: there are three. | Extract one digit at a time with MOD 10, increment CONT only when it is 7 and continue with DIV 10. |
To count a chosen digit, replace 7 with a parameter C between 0 and 9. Be careful with the number 0: the digit 0 occurs in it once, not zero times. Handle it separately before the loop; for any other digit the count in 0 is zero.
17. Checking whether all digits are equal
Problem. Check whether all digits are equal in 5555 and in 5553.
| Human solution | Machine solution |
|---|---|
Recognise four 5s in the first number; notice the different 3 in the second. | Save the first digit extracted with MOD 10 and compare each remaining digit with it. Stop at the first mismatch. |
For 5555 the reference digit is 5 and every comparison confirms equality. For 5553 the reference is 3 and the next 5 disproves it: no need to continue. The procedure does not require knowing the number of digits in advance. A single-digit number, including 0, satisfies the property.
Part three — Three meaningful applications
The same ideas do more than manipulate digits. Let us transfer them to times, searches and error checking: each time we specify data, procedure and verification.
18. Times: when division describes a real situation
Problem. A lesson starts at 09:50 and lasts 95 minutes. When does it end?
| Human solution | Machine solution |
|---|---|
Split 95 minutes into 60 + 35: one hour later it is 10:50; another 35 minutes takes us to 11:25. | Convert the start into minutes since the beginning of the day, add the duration, then convert back with DIV 60 and MOD 60. Allow hours from 0 to 23, minutes from 0 to 59 and a non-negative integer duration. |
The lesson ends at 11:25. DIV 60 counts complete hours and MOD 60 the remaining minutes: the same idea we used for digits, but with groups of 60.
Boundary case: 23:50 plus 30 minutes gives 1460 minutes. The quotient when dividing by 1440 tells us how many days have passed; the remainder gives the minutes in the new day. We obtain 00:20 the next day. Stating the day change is part of the solution, not a cosmetic detail.
23 × 60 + 50 + 30 = 1460
1460 DIV 1440 = 1
1460 MOD 1440 = 20
20 DIV 60 = 0
20 MOD 60 = 20
19. Searching for a number: why representation changes the method
Problem. Find 42 in the sorted list [3, 8, 12, 17, 23, 31, 42, 58].
| Human solution | Machine solution |
|---|---|
Scan from the left and recognise 42 in the seventh position: this strategy examines seven elements. | Use the ordering for binary search: number positions from 1, choose the middle position rounded down and discard the half ruled out by the comparison. Visit positions 4, 6 and 7. |
| Comparison | Next interval |
|---|---|
| Position 4: 17 < 42 | Positions 5–8 |
| Position 6: 31 < 42 | Positions 7–8 |
| Position 7: 42 = 42 | Found: position 7 |
Only three comparisons were needed. If we searched for 40, after comparing with 42 we would have an empty interval and could declare the number absent. The stopping condition therefore includes both ‘found’ and ‘no positions left’.
This search works because the list is sorted and we can access its middle positions. In an unsorted list, discarding half the values would be unjustified. Sorting has a cost: it may not pay off for a single search; for many searches, the same sorted data can be reused. A good algorithm also depends on assumptions and context.
20. Finding a bug: the maximum of negative numbers
Problem. Why does starting the maximum at M = 0 work for [17, 42, 29] but fail for [−8, −3, −12]? How can we correct it?
| Human solution | Machine solution |
|---|---|
Recognise −3 as the largest of the three negative numbers. Notice that 0 is not in the list and cannot be its maximum. | With M = 0 no negative number updates the maximum, so the result incorrectly stays 0. For a non-empty list, initialise M with the first element instead, then compare the others. |
The correct result is −3. Testing positive numbers was not enough to establish the procedure’s validity. Also try a single-element list, equal values and an empty list: in the last case the maximum is undefined and we must report missing data, not invent a zero.
This is also a useful habit with AI: ask not only for a solution, but for its assumptions, a counterexample and test cases. A convincing answer still has to pass independent checks.
Why discuss this in ‘AI and thinking’?
Knowing how to formulate a procedure also helps us use artificial intelligence without confusing a plausible answer with a verified one. We can ask for a strategy, but we must know what data it uses, what assumptions it requires and how to check the result.
A useful request is: ‘I need to count the digits of an integer, including zero. Show the steps for 72845, explain the stopping condition and also test 0 and −10, counting only digits and not the sign.’ This specifies the input, desired result, explanation method and boundary cases. It does not guarantee correctness by itself, but it puts us in a position to check it.
A short checklist
- What are the inputs and which values are allowed?
- What result do I want, and in which representation?
- Which operations can I use and what information must I preserve?
- Which instructions repeat, and why does the repetition terminate?
- What happens with zero, minimal inputs, missing data or unexpected values?
- Can I execute the procedure by hand and check it using another method?
Conclusion — From an answer to a procedure
Many operations are immediate for us because we see the written representation of a number. When looking for a procedure, we must make explicit which data to use, which operations to perform, when to repeat them and when to stop.
The educational aim is not to abandon intuition, but to turn it into a method: ‘How did you find the result? Does the procedure always work? Can you explain it to someone who cannot rely on your eyes?’ These questions connect mathematics, logic and computing and can be explored long before learning a programming language.