Given two numbers $x \gt 0$ and $y \gt 0$, find the greatest number $a$ such that $a\mid x$, and $a$ and $y$ are $coprimes$.
A possible solution is given here. The idea is to divide $x$ with $\gcd(x,y)$ until $\gcd(x,y)$ becomes $1$. Whatever is remaining of $x$ is the answer.
I want to understand why this solution works. How is it guaranteed to give the greatest $a$ such that $a\mid x$?
Informal but intuitive:
We can break $x$ done into three components: $A = $ the stuff in $x$ that has nothing to do with $y$. (This is clearly the answer to our question.) $D= \gcd(x,y) =$ the stuff $x$ and $y$ have in common and the exact amount they share. And $E = $ the stuff in common with $y$ that $x$ has too much of.
So $x = A*D*E$.
We do your algorithm. $w = \frac {x}{\gcd(x,y)} = \frac {A*D*E}{D} = A*E = $ the stuff that has nothing to do with $y$ $\times$ the stuff in common with $y$ that $x$ has too much of.
$\gcd(w,y) = \gcd($ the stuff that has nothing to do with $y$ $\times$ the stuff in common with $y$ that $x$ has too much of$, y) = $ the stuff $x$ had too much of (either the excessive amount or the amount in $y$ whichever is smaller).
So $$z = \frac {w}{\gcd(w,y)} = \frac {\text{the stuff that has nothing to do with }y\times\text{ the stuff in common with }y\text{ that }x\text{ has too much of}}{\text{the stuff x had too much of (either the excessive amount or the amount in y whichever is smaller)}}$$ = the stuff that had nothing to do with $y$ $\times$ the extra stuff that $x$ had too much of if any is left.
If there is any stuff that $x$ had too much of we continue.
$\gcd(y, z) = \gcd(y, $the extra stuff that $x$ had too much of if any is left$))$ = some smaller amount of the extra stuff that $x$ had.
And $u = \frac z{\gcd(y, z)} = \frac{\text{the stuff that had nothing to do with }y \times\text{ the extra stuff that x had too much of if any is left}}{\text{ some smaller amount of the extra stuff that x had}}$ = the stuff that had nothing to do with $y$ $\times$ a smaller amount (possibly none) of that extra stuff.
We keep repeating until we don't have any more of the extra stuff that $x$ had.
Then we are left with $a = $the stuff that had nothing to do with $y = A$.