The Euclidean GCD Algorithm is an algorithm that efficiently computes the GCD of two positive integers, by repeatedly subtracting the smaller number from the larger number until they become equal. It can be visualised as such:
We start with a MxN grid, where M and N are the numbers. In this example I'll use M=12, N=5:
12
____________
|
|
5|
|
|
We then, going from the top left, remove a square with a side length of the smaller of the two values, 5, leaving a 7x5 rectangle. This is visualised by replacing the last column of the 5x5 square with a column of |
s.
7
____________
| |
| |
5| |
| |
| |
We then remove another 5x5 square, leaving a 2x5 rectangle:
2
____________
| | |
| | |
5| | |
| | |
| | |
Next, we remove two 2x2 squares, leaving a 2x1 rectangle. As these squares are added when the rectangle is taller than it is wide, their bottom rows are replaced with rows of _
s.
2
____________
| | |
| | |__
| | |
| | |__
1| | |
And finally, we remove a 1x1 square, leaving a 1x1 square, whose last and only column is replaced with a |
. As this is a square, the two sides are equal, so the GCD of 12 and 5 is 1.
The final visualisation looks like this. Note that the numbers are not included, those were just visual indicators.
| |
| |__
| |
| |__
| ||
Here's a larger example for M=16, N=10. (I'm not going to go through all the steps here).
|
|
|
|
|
|______
| |
| |__
| |
| |
Note that, as gcd(16, 10) = 2, the algorithm ends with a 2x2 square.
Your challenge is to implement this visualisation, given two numbers M and N where M > N. Your output may have any amount of leading/trailing whitespace.
Testcases
12, 5 ->
| |
| |__
| |
| |__
| ||
10, 16 ->
|
|
|
|
|
|______
| |
| |__
| |
| |
25, 18 ->
|
|
|
|
|
|
|_______
|
|
|
|
|
|
|_______
| |
| |
| |___
| |||
25, 10 ->
| |
| |
| |
| |
| |_____
| |
| |
| |
| |
| |
34, 15 ->
| |
| |
| |
| |____
| |
| |
| |
| |____
| |
| |
| |
| |____
| | |_
| | |_
| | |
89, 55 ->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|__________________________________
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |_____________
| | |
| | |
| | |
| | |
| | |_____
| | | |
| | | |__
| | | ||