NCL 111: Working with Numbers

So far, we've learned how to store numbers, change them, compare them, choose different paths through a program, and repeat instructions.

This time, we're not going to introduce another operation.

Instead, we're going to take an existing algorithm and turn it into a program.

The problem

You've probably found greatest common divisors by hand before.

The greatest common divisor, or GCD, of two integers is the largest integer that divides both values evenly.

For example, 48 and 18 are both divisible by:

1
2
3
6

The greatest of those is 6, so:

gcd(48, 18) = 6

Finding a GCD by hand isn't especially difficult, but it can become tedious when the numbers get larger.

Fortunately, the computer doesn't mind tedious.

If we can describe a procedure using operations the CPU understands, we can let it do the repetitive work for us.

Euclid's algorithm

We could find the GCD by testing possible divisors one at a time.

Instead, we'll use Euclid's algorithm, a procedure for finding the GCD of two integers.

Why does Euclid's algorithm work?

The key is that replacing two numbers with the smaller number and their remainder does not change their greatest common divisor.

Suppose:

a = q × b + r

Any number that divides both a and b must also divide r, because r is what's left after subtracting some whole number of bs from a.

The reverse is also true: any number that divides both b and r must divide a.

So:

gcd(a, b) = gcd(b, r)

Each step replaces the original pair with a smaller pair that has exactly the same GCD.

Eventually the remainder becomes 0. At that point:

gcd(a, 0) = a

and the value left in a is the answer.

The algorithm works like this:

  1. Start with two values, a and b.
  2. Find the remainder of a divided by b.
  3. Replace a with the old value of b.
  4. Replace b with the remainder.
  5. Repeat until b becomes zero.
  6. The value left in a is the GCD.

Let's try it with:

a = 1071
b = 462

The first remainder is:

1071 MOD 462 = 147

So the next pair becomes:

a = 462
b = 147

Then:

462 MOD 147 = 21

giving us:

a = 147
b = 21

One more step gives:

147 MOD 21 = 0

and:

a = 21
b = 0

Now b is zero, so the algorithm is finished. The GCD is 21.

The full process looks like this:

a b a MOD b
1071 462 147
462 147 21
147 21 0
21 0 —

Each step applies the same procedure to a new pair of values.

Now we need to make the computer do it.

Turning the algorithm into instructions

The algorithm needs to keep track of three values:

  • the current value of a;
  • the current value of b;
  • the remainder we just calculated.

We'll use three memory locations:

#a $000
#b $001
#remainder $002

Then give #a and #b their starting values:

MOVE #a 1071
MOVE #b 462

The first step of the algorithm says to find the remainder of a divided by b.

We already have MOD:

MOD #remainder #a #b

Then a takes the old value of b:

MOVE #a #b

and b takes the remainder:

MOVE #b #remainder

Together, one step of Euclid's algorithm becomes:

MOD #remainder #a #b
MOVE #a #b
MOVE #b #remainder

Starting with:

#a #b #remainder
Before 1071 462 —
After 462 147 147

Compare that with the first two rows of our manual trace:

a b a MOD b
1071 462 147
462 147 21

We've translated one step of the algorithm into NCL.

Repeat the step

Euclid's algorithm repeats that same step until b becomes zero.

We already know how to repeat instructions:

:again
MOD #remainder #a #b
MOVE #a #b
MOVE #b #remainder
JUMP :again

This repeats the calculation, but it never stops.

The algorithm tells us exactly when it should stop:

Repeat until b becomes zero.

We can test that at the beginning of each iteration:

:again
BEQ :done #b 0

MOD #remainder #a #b
MOVE #a #b
MOVE #b #remainder
JUMP :again

If #b isn't zero, execution continues through another step and jumps back to :again.

When #b is zero, BEQ branches to :done.

Display the result

When the algorithm finishes, the value left in #a is the GCD.

So :done only needs to display it:

:done
D.TXT #a
D.BLT

Put everything together:

#a $000
#b $001
#remainder $002

MOVE #a 1071
MOVE #b 462

:again
BEQ :done #b 0

MOD #remainder #a #b
MOVE #a #b
MOVE #b #remainder
JUMP :again

:done
D.TXT #a
D.BLT

Run it.

The Display shows:

21

We've taken the steps of Euclid's algorithm and expressed each one using instructions the CPU already understands.

Following the loop

The changing values can be summarized with a trace:

#a #b #remainder Next action
1071 462 — Continue
462 147 147 Continue
147 21 21 Continue
21 0 0 Branch to :done

Each trip through the loop uses the values produced by the previous one.

When #b finally contains 0, the branch is taken before another MOD is performed, and #a contains the result.

Try different values

The program isn't limited to 1071 and 462.

Change the starting values:

MOVE #a 48
MOVE #b 18

The result is:

6

Try:

MOVE #a 17
MOVE #b 13

The result is:

1

You can also try zero:

MOVE #a 0
MOVE #b 462

which produces 462.

Starting with:

MOVE #a 462
MOVE #b 0

also produces 462.

If both values begin as zero, this program produces zero.

Try some starting values of your own. The instructions that perform the algorithm don't need to change.

An algorithm as a program

We started with a procedure:

  1. Calculate a remainder.
  2. Update two values.
  3. Repeat until one of them reaches zero.

Every part of that procedure could be expressed using instructions we've already learned.

Memory locations hold the changing values. MOD calculates the remainder. MOVE updates the values. BEQ detects when the algorithm is finished, and JUMP repeats the work.

The same program can then perform the algorithm with different starting values.


In the 100-level, we've learned how a program can remember values, change them, make decisions from them, and repeat work until a condition is reached.

In this lesson, we used those pieces together to turn an algorithm into a program.

Ready to continue? In the next lesson, NCL 201: Remembering Text, we'll start giving the computer something else to remember: text.

Want to try one yourself first? The 100-Level Challenge gives you another algorithm to turn into an NCL program.