You may also like

problem icon

First Forward Into Logo 1: Square Five

A Short introduction to using Logo. This is the first in a twelve part series.

problem icon

First Forward Into Logo 10: Count up - Count Down

What happens when a procedure calls itself?

problem icon

First Forward Into Logo 2: Polygons

This is the second in a twelve part introduction to Logo for beginners. In this part you learn to draw polygons.

Mystery Procedure

Stage: 4 Challenge Level: Challenge Level:1

This procedure calculates the highest common factor of A and B. 


The algorithm swaps over A and B to ensure A > B. We know hcf(A,B) $\leq$ min(A,B) = B in this case. Therefore, the first guess for N = hcf(A,B) is B. It first tests whether N is a factor of B. If that's the case, it then tests whether it's a factor of A. If either of these fail, it decreases N by 1. Eventually, it will either stop at a value of N>1 such that N divides A and B, or will terminate when N=1 (as A/1 and B/1 are both whole numbers). In the latter case, A and B are coprime.