17
3
The Euclidean algorithm is a widely known algorithm for calculating the greatest common divisor (GCD) of two positive integers.
The algorithm
For the purpose of this challenge, the algorithm is described as below:
Display the two input as adjacent lines of a certain character
e.g. an input of3,4
can be represented by the adjacent lines000
and0000
Turn the first
length(short_line)
characters in the longer line into another character, say-
now it looks like000
and---0
Eliminate the first
length(short_line)
characters in the longer line.
now000
,0
Repeat step 2 and 3 until the two have equal length, using the shorter and longer lines after each iteration, e.g.
000
,0
-00
,0
00
,0
-0
,0
0
,0
- You can choose whether to stop here or continue the iteration and turn one of the lines into an empty line.
Each of these steps should be separated by an interval between 0.3s and 1.5s.
The challenge
Write a program that, given two natural numbers as input, creates an output that looks exactly the same as the output of the algorithm above. You can use other non-whitespace printable ASCII characters than 0
and -
, but be consistent and use only two characters. You can also use alternative algorithms provided the output, including the timing, is exactly the same as would be produced by the algorithm above.
Examples
This is an example with input 24,35
, which are coprimes so their GCD is 1.
This is an example with input 16,42
, which have the GCD 2.
Rules
- This is a code-golf, so shortest bytes wins
- Standard loopholes apply
- You can assume input to be positive decimal integers
Clarifications
- The lines that represent the numbers need to stay in their original order, i.e. the first and second lines of the first displayed "frame" need to be the first and second lines respectively, in all subsequent frames.
- After the algorithm ends, no additional visible entity should appear. However, this also means that it is okay to blank the lines, if you make sure that the last "frame" is displayed for at least the same amount of time as did all other frames before blanking out.
@WheatWizard great suggestion, on it – busukxuan – 2017-02-18T14:48:33.127
Do the lines have to stay in the same relative order? Or can they be reordered between iterations? (Checking because the latter is likely to be much more concise in most languages, and thus I need to know whether I should use that optimization or ignore it due to violating the sepc.) – None – 2017-02-18T15:12:12.770
@ais523 Yes they do
:-)
– busukxuan – 2017-02-18T15:17:56.260@ais523 Yes it's okay to blank it, but make sure the last frame is given the same display time as the other frames – busukxuan – 2017-02-18T15:31:39.577
Can we take the input in unary? (that may save one step of converting from number to sequence of characters) – Luis Mendo – 2017-02-18T16:03:38.143
@LuisMendo Sorry, but no you can't. Thanks for finding another possible loophole :-) The sandbox doesn't really give too much attention to loopholes – busukxuan – 2017-02-18T16:08:19.703
@busukxuan It makes sense for that to be banned :-) Can
-
be replaced by space? Again, that might help, but it would be the same char as possible trailing spaces of the shorter string – Luis Mendo – 2017-02-18T16:14:43.547@LuisMendo I would actually like space to be legal, but I'm not sure if it's a good idea to allow it for the
-
replacement only, or if it's more "elegant" to just ban it for both characters. – busukxuan – 2017-02-18T16:22:16.507Not a duplicate, despite the name, but definitely related; both questions are about visualising GCD algorithms, they just picked entirely different algorithms to visualise. – None – 2017-02-18T16:29:00.973
1@busukxuan Personally I think I would allow trailing spaces, but perhaps not space as one of the "meaningful" characters – Luis Mendo – 2017-02-18T17:08:14.417
Are you overriding this meta consensus regarding unary input? I.e is unary input banned for sed answers?
– Digital Trauma – 2017-02-18T22:09:48.820@DigitalTrauma Wow, didn't know that existed! Well, it seems more unfair to ban them than to allow exceptions, so it's ok if you're sed. – busukxuan – 2017-02-19T04:25:44.897
Is it mandatory to clear the screen between outputs? Generally I tend to keep this optional in my challenges as I don't really think
print '\n'*50
adds much value to the challenge. No criticism intended. Just seeking clarification. – ElPedro – 2017-02-19T13:52:35.450@ElPedro No, only the two lines are relevant – busukxuan – 2017-02-19T13:53:26.737