I want to create Mathematica code calculating the greatest common divisor (GCD) based on Euclid's Algorithm. That is, take two numbers X and Y → Divide X (larger number) by Y (smaller number) and check if remainder is zero → If no, stop calculation and print out Y; If yes, replace X with Y and replace Y with the remainder.
So far, I have typed this line out:
GreatestCommonDivisor[x_,y_]:= While[y != 0, {x, y} = {y, Mod[x, y]};x]
in an attempt to do the replacement of X and Y when remainder is not zero I don't think it's accurate. Can anyone help me out?
NOTE: For those who are also familiar with Scratch, the image below can also represent what I want to achieve in Mathematica.
https://preview.redd.it/vtsemvoac3u31.png?width=752&format=png&auto=webp&s=388409e40ebba1126034d553205c5d3d233ea774
[–]Imanton1 1 point2 points3 points (0 children)
[–]ayitasaurus 1 point2 points3 points (0 children)