You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here, we simply line the two numbers up every step and subtract the lower value from the higher one every timestep. Once the two values are equal, we call that value the greatest common divisor. A graph of `a` and `b` as they change every step would look something like this:
@@ -132,6 +134,8 @@ Modern implementations, though, often use the modulus operator (%) like so
Here, we set `b` to be the remainder of `a%b` and `a` to be whatever `b` was last timestep. Because of how the modulus operator works, this will provide the same information as the subtraction-based implementation, but when we show `a` and `b` as they change with time, we can see that it might take many fewer steps:
so basically this is the modulo algorithm in brainfuck, it slowly shifts cell 2 to cell 3, while subtracting 1 from cell 1
299
+
then when cell 2 goes to 0, it shifts cell 3 to 2 and continues, this is like just constantly subtracting cell 2 from cell 1, until you cant subtract anymore then return at cell 3
300
+
301
+
State: `0 >0 b-a%b a%b 0 0`
302
+
303
+
shifting: `>[-<+>]`
304
+
305
+
State: `0 b-a%b >0 a%b 0 0`
306
+
307
+
Currently we have a,b,0=b-a%b,0,a%b, and we need a,b,0=b,a%b,0, so we just add the third cell to the first and second cell
308
+
309
+
adding thing: `>[-<+<+>>]<`
310
+
311
+
State: `0 b >(a%b) 0 0 0`
312
+
313
+
So now we have a,b=b,a%b, we continue the loop
314
+
315
+
`]`
316
+
317
+
After the second cell is 0, the loop terminates and we obtain the GCD
0 commit comments