Created
November 16, 2013 05:45
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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