Learnify Academy is a tuition centre in Bahrain. Classes are for students in Bahrain only.Tuition classes in Bahrain only

Algorithms · 3 marks

Describe an algorithm to compute the least common multiple (lcm) of two numbers.

Answer: Check the multiples of the larger number in order: big, 2 × big, 3 × big, …; the first one that the smaller number divides is the lcm. It is always found by small × big. Example: lcm(12, 18): 18 ✗, 36 ✓, so lcm = 36.

Step-by-step solution

Idea: The lcm is the smallest number that is a multiple of both. Every common multiple is a multiple of the larger number, so test the multiples of the larger number from the smallest upward and stop at the first one the smaller number divides.

  1. Algorithm lcm(m, n)
    1. Let big = max(m, n) and small = min(m, n).
    2. For each k in the sequence 1, 2, 3, …, small:
      – if small divides k × big, report k × big as lcm(m, n) and stop.1½ marks
  2. Why it is correct: the numbers k × big are exactly the multiples of big, checked from smallest to largest. The first one that small divides is a multiple of both numbers, and no smaller common multiple was skipped, so it is the least common multiple.½ mark
  3. Why it always stops: when k = small, k × big = m × n, which both numbers divide. So the algorithm stops at the latest at k = small.½ mark
  4. Example: lcm(12, 18). big = 18, small = 12. k = 1: 18, 12 does not divide it. k = 2: 36, 12 divides it (36 = 3 × 12). Report lcm(12, 18) = 36.½ mark
  5. Another way (using the gcd algorithm): lcm(m, n) = (m × n) ÷ gcd(m, n). For 12 and 18: 216 ÷ 6 = 36, the same answer.
Test big, 2 × big, 3 × big, … (big = the larger number) and report the first one divisible by the smaller number; it is found by k = small at the latest. For example lcm(12, 18) = 36.

Check: 36 ÷ 12 = 3 and 36 ÷ 18 = 2, and the only smaller multiple of 18 is 18, which 12 does not divide ✓. Also 12 × 18 ÷ gcd(12, 18) = 216 ÷ 6 = 36 ✓.

Answer to write in the exam

Algorithm lcm(m, n):

1. big = max(m, n), small = min(m, n)

2. For each k = 1, 2, 3, …, small:

– if small divides k × big, report k × big as lcm(m, n) and stop

(Stops by k = small, since m × n is a common multiple.)

e.g. lcm(12, 18): 18 ✗, 36 ✓

∴ lcm(12, 18) = 36

Common mistakes that cost marks

  • Reporting m × n as the lcm. It is a common multiple but not always the least (12 × 18 = 216, but the lcm is 36).
  • Checking multiples of the smaller number against the larger one but starting the count wrongly, so a smaller common multiple is skipped.
  • Mixing up lcm and gcd: the lcm is at least as big as the larger number; the gcd is at most the smaller number.

How this can come in the exam

MCQ (1 mark)

The lcm algorithm tests 20, 40, 60, … in turn to find lcm(15, 20). How many multiples of 20 does it test?

  1. 1
  2. 2
  3. 3
  4. 4
Show answer

(C) 3
20 ✗ (15 does not divide 20), 40 ✗, 60 ✓. Three tests; lcm(15, 20) = 60.

Try one yourself

Use the algorithm to find lcm(14, 21).

Show answer

big = 21, small = 14. 21: 14 does not divide it. 42: 14 divides it (42 = 3 × 14). lcm(14, 21) = 42.

More questions like this

All Algorithms questions · All maths questions