The greatest common divisor between two numbers is defined as the largest value that can divide both numbers.
To compute the greatest common divisor there are many algorithms. One of the fastest is over 2 millennium old. It's called Euclid's algorithm. Here you will be able to learn more about this algorithm and how to program it. Also you will learn how to visualize the number of iterations it needs to find the result.
The algorithm could consist of the following points:
- Let a and b be the two numbers for which we are trying to find the GCD
- Initialize d at 1 and solution also at 1. (Notice 1 divides any number)
- If d divides a and also d divides b, then store d in solution.
- Increase d in one unit
- If d is smaller than a and b repeat step 3.
- The result will be stored in solution.
Let us take a moment to discuss some of the instructions used in this small code.
First of all to read and write in eSeeCode you need to go to the I/O tag next to the Debug. To read some values we will use the instruction input() and, to write, we use the instruction output().
Also, as in many languages, d++ means increase the value in d by one unit.
Finally to check if d divides a we use the operator %. This will return the reminder of the division between a and d. As an example a%2==0 would mean a is an even number.
Euclid's algorithm is based on a simple fact. Let (a,b) stand for the greatest common divisor between a and b. Then (a, b) = (a, b-a). You can try to prove this statement by considering the set of divisors of each side and showing they are the same set. Generalizing this result we can state the fact that (a,b)=(b,a%b).
Becuase this is a recursive definition we need to define a basic case. In this case it will be when b is 0 (the greatest common divisor will be a). This property gives us an outline for a recursive algorithm. A recursive function is a function that calls itself, and to avoid an infinite loop we need a base case. This is similar to a proof by induction.
We can use this function to create a representation of Euclid's algorithm. Using the Canvas we can draw squares to simulate the process that is happening. Try changing the values in the I/O tag to see how the algorithm reacts to different inputs.We are interested in studying the number of calls that our recursive function does. To do so we could build a table for each pair of numbers in a certain range. This would give us an idea on how it changes, but because the variation is slow, it would be tedious and we would need plenty of values to see the behavior. Instead we will represent the number of iterations in a plane.
For a given point (x,y) we will compute how many calls we need to compute the greatest common divisor of x and y. Then we will paint that point with a specific color depending on the number of steps. To simplify the program we will consider the number steps of x and y the same as for y and x, although they differ by one unit. In our case we will use a grid of 400x400 points. The result can be seen in this figure.
The program to do this can be seen here. We might need to increase the time allowed to run the program depending on the browser. You can do this in the
Notice that we have used the for loop. This is faster than the repeat loop, and you can control the current step in each loop because you can always call the for variable (i in the first loop and j in the inner loop).
We might want to highlight the important lines from this graph.
In black we have painted the lines y=kx and the lines y=(1/k)x, where k is a positive integer. We can see that those lines correspond to the same color in figure 1. This is because it only needs one iteration to find the answer.
In yellow we have marked the Fibonacci Numbers and the line y=Phi*x, where Phi refers to the golden ratio. We can see in Figure 1 that this corresponds with the most number of calls. Try to figure out why do you think that this is the case.
As in the previous case, we might want to increase the time allowed to execute the program.
What other ideas would you investigate in this topic? As always any suggestions are welcome. Send us an e-mail at info@eseecode.com or twit us at @eseecode or leave a comment below.