📘 CodingMarble Learn

Number Algorithms

A number algorithm is a short list of steps a computer repeats to work with numbers. Use n % 10 and n ÷ 10 to take digits one by one. Find divisors in pairs, split a number into primes, find the HCF with Euclid's method (replace (a, b) by (b, a mod b)), and change base by dividing again and again.

🎬 Step-by-step story

  1. Every number is a row of digits. Tap the button. n % 10 gives the last digit and n ÷ 10 keeps the rest. Repeat until n is 0.
  2. 12 blocks make a rectangle in 3 ways: 1 × 12, 2 × 6, 3 × 4. So 1, 2, 3, 4, 6 and 12 divide 12 exactly. They are its divisors. 12, 24, 36 are its multiples.
  3. Split 12 again and again. Stop when only prime numbers (gold balls) are left. 12 = 2 × 2 × 3. This is prime factorisation.
  4. Euclid's trick: cut the biggest squares you can from a 48 by 18 rectangle. Keep cutting what is left. The last square has side 6. So the HCF of 48 and 18 is 6.
  5. Binary uses only 0 and 1. 13 = 8 + 4 + 1, so lamps 8, 4 and 1 are on. Dividing by 2 again and again and keeping the remainders gives the same bits.
  6. Free play: move the slider to any number from 0 to 63. Read its binary lamps, its digit sum and its prime factors.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why does the digit loop stop?

Each n ÷ 10 removes one digit. After the last digit is taken, n becomes 0 and the loop ends. Watch the n label go to 0.

Why do I only test divisors up to the square root?

Divisors come in pairs, like the three rectangles of 12. One side of each pair is always small, so finding the small side gives the big side for free.

Is 1 a prime number?

No. A prime has exactly two divisors. In the factor tree every end ball is a prime; 1 is never one of them.

Why does the last square give the HCF?

Its side fits exactly into every earlier square and the full rectangle, so it is a common divisor. No bigger square could do that.

Why does binary use only 0 and 1?

A lamp is either on or off. Each place value is double the one before (1, 2, 4, 8…), and every number is a sum of some of them.

Can every number be written in binary?

Yes. Move the slider: every number from 0 to 63 is a different pattern of the six lamps.

Taking digits of a number

A computer cannot look at a number like you do. It peels digits off one at a time with two small tools.

Repeat while n is not 0. Add each digit you take to a running total to get the sum of digits: 2 + 7 + 4 = 13. Count the loops to get the number of digits. Build rev = rev × 10 + digit to get the reverse of the number.

Why it stops

Each loop makes n about ten times smaller. After as many loops as there are digits, n becomes 0 and the loop ends.

Divisors, multiples and primes

A divisor of n divides n with remainder 0. A multiple of n is n × 1, n × 2, n × 3 and so on.

Divisors come in pairs: if 2 divides 12 then so does 12 ÷ 2 = 6. So a program only needs to test numbers up to the square root of n. For 36 the pairs are (1, 36), (2, 18), (3, 12), (4, 9) and the single 6.

A prime has exactly two divisors: 1 and itself (2, 3, 5, 7, 11…). The number 1 is not prime. A number with more divisors is composite.

Prime factorisation

Every number bigger than 1 can be written as a product of primes in exactly one way (apart from order). To find it, divide by the smallest prime that works, again and again.

Example: 360 ÷ 2 = 180, ÷ 2 = 90, ÷ 2 = 45, ÷ 3 = 15, ÷ 3 = 5, ÷ 5 = 1. So 360 = 2³ × 3² × 5.

Use it to count divisors: add 1 to each power and multiply. For 360 that is (3 + 1)(2 + 1)(1 + 1) = 24 divisors.

Euclid's algorithm for HCF

The HCF (highest common factor, also called GCD) is the biggest number that divides both a and b.

By subtraction

Replace the bigger number by the difference of the two. Repeat until the numbers are equal. HCF(48, 18): 48 − 18 = 30, 30 − 18 = 12, 18 − 12 = 6, 12 − 6 = 6. The equal pair is (6, 6), so the HCF is 6. In the 3D this is cutting squares.

By division (faster)

Write a = q × b + r. Then HCF(a, b) = HCF(b, r). Stop when r = 0; the last divisor is the HCF. 48 = 2 × 18 + 12, 18 = 1 × 12 + 6, 12 = 2 × 6 + 0, so the HCF is 6.

Bonus: LCM = a × b ÷ HCF.

Changing number base

Our usual numbers are base 10: ten digits 0 to 9. Computers use base 2 (binary): only 0 and 1. In base b, the digits of a number are place values that are powers of b.

Decimal to base b

Divide by b again and again. Write the remainders. Read them from last to first. 13 → 13 ÷ 2 = 6 r 1, 6 ÷ 2 = 3 r 0, 3 ÷ 2 = 1 r 1, 1 ÷ 2 = 0 r 1. Read upward: 1101.

Base b to decimal

Multiply each digit by its place value and add. 1101₂ = 8 + 4 + 0 + 1 = 13.

Try it: an algorithm with matchsticks

Take 48 matchsticks and 18 pins. Make the biggest equal groups with nothing left over. Count how many groups you can make. Check it with Euclid's method. Then write 13 as lamps (8, 4, 1) with a friend: who can write their age in binary first?

Key formulas and definitions

Worked examples

1. Find the sum of the digits of 2468 using the n % 10 method.

2468 % 10 = 8, n = 246. 246 % 10 = 6, n = 24. 24 % 10 = 4, n = 2. 2 % 10 = 2, n = 0. Sum = 8 + 6 + 4 + 2 = 20.

2. Reverse the number 1234 with rev = rev × 10 + digit.

rev = 0. Digit 4: rev = 4. Digit 3: rev = 43. Digit 2: rev = 432. Digit 1: rev = 4321. The reverse is 4321.

3. List all divisors of 36.

Test 1 to 6 (the square root): 1, 2, 3, 4, 6 divide 36. Their partners are 36, 18, 12, 9, 6. So the divisors are 1, 2, 3, 4, 6, 9, 12, 18, 36 (nine of them).

4. Write 360 as a product of primes and count its divisors.

360 = 2 × 2 × 2 × 3 × 3 × 5 = 2³ × 3² × 5. Divisors = (3 + 1)(2 + 1)(1 + 1) = 24.

5. Find HCF(84, 36) by the division method, then the LCM.

84 = 2 × 36 + 12. 36 = 3 × 12 + 0. The last divisor is 12, so HCF = 12. LCM = 84 × 36 ÷ 12 = 252.

6. Convert 45 to binary.

45 ÷ 2 = 22 r 1; 22 ÷ 2 = 11 r 0; 11 ÷ 2 = 5 r 1; 5 ÷ 2 = 2 r 1; 2 ÷ 2 = 1 r 0; 1 ÷ 2 = 0 r 1. Read upward: 101101. Check: 32 + 8 + 4 + 1 = 45.

7. Convert 11010 (binary) to decimal.

Place values from the right: 1, 2, 4, 8, 16. 1·16 + 1·8 + 0·4 + 1·2 + 0·1 = 16 + 8 + 2 = 26.

8. Convert 45 to base 5.

45 ÷ 5 = 9 r 0; 9 ÷ 5 = 1 r 4; 1 ÷ 5 = 0 r 1. Read upward: 140 in base 5. Check: 25 + 4 × 5 + 0 = 45.

Common mistakes

Practice quiz

1. Which expression gives the last digit of n?
2. What is HCF(48, 18)?
3. Which number is prime?
4. 13 in binary is:
5. In Euclid's method, when do we stop?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is Euclid's algorithm in simple words?

It finds the HCF of two numbers by dividing the bigger by the smaller, then the smaller by the remainder, and so on until the remainder is 0. The last divisor is the HCF.

How do I find the sum of digits of a number?

Repeat: add n % 10 to a total, then set n = n ÷ 10, until n is 0.

How do I convert a decimal number to binary?

Divide by 2 again and again, note each remainder, and read the remainders from last to first.

Where this is taught

RomaniaClasa a IX-aProblem-solving strategies
RomaniaClasa a IX-aProblem-solving strategies
RomaniaClasa a IX-aProblem-solving strategies

Learn first

Learn next

Related lessons

All Computer Science lessons