I don't know OCaml, but I know enough similar languages that I think I can read that well enough to answer. Still, take this with a grain of salt.
That is tail recursive. You either return a value, or return the result of a recursive call that depends only on its input parameters that are known at the time of the call.
There is a more efficient algorithm. Hint: repeated subtraction. (That may not be the most efficient algorithm.)
You don't actually use the parameter n, you could just use x directly in the same way you use y directly. The name rem
isn't the most descriptive; remainder
would be better, but that particular function is mostly known as mod
or modulus
.
I would find this formatting more readable:
if i = n
then acc
else if acc+1 = y
then aux 0 (i+1) n
else aux (acc+1) (i+1) n
mod
function using sequence of additions your solution can be cheaper for whenx div y
is low and more expensive thanx mod y
whenx div y
is large because built-inmod
probably can be very fast if suitabe processor instruction exists. \$\endgroup\$