The following loop fails to terminate and I have no idea why. I've either violated some syntax rule, there's an issue with the self reference in n:= floor (n/2);, or I've missed something embarrassingly obvious.
\documentclass{article}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
def divtwo(expr n)=
forever: exitunless n>1;
n:= floor (n/2);
endfor;
enddef;
divtwo(5);
end;
\end{mplibcode}
\end{document}
Edit: Thanks for the answers! The actual use case was to find the binary digits of a number (order irrelevant). Corrected (probably still bad) code:
\documentclass{article}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
numeric k[];
def divtwo(expr n)=
save m;
m:=n;
j = 1;
forever: exitunless m>0;
b:= m mod 2;
m:= floor (m/2);
k[j]:=b;
j:=j+1;
endfor;
enddef;
divtwo(5);
end;
\end{mplibcode}
\end{document}

save min adef. Either use a group (withvardef, probably) or a specific variable you don't use anywhere else. – egreg Feb 16 at 22:52