Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Created November 16, 2013 05:45
Show Gist options
  • Save animatedlew/7496438 to your computer and use it in GitHub Desktop.
Save animatedlew/7496438 to your computer and use it in GitHub Desktop.
This is my implementation of the modulo operator. The function first checks to see if the result is positive. If it is, it generates a new dividend by subtracting the divisor, thereby narrowing down to the remainder, which will be left in the dividend.
def modulo(dividend: Int, divisor: Int): Int = {
if (dividend / divisor > 0) modulo(dividend - divisor, divisor) else dividend
}
def %(money: Int, coin: Int): Int = {
money % coin
}
println(%(4, 3))
println(modulo(4, 3))
/*
1
1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment