Last active
May 13, 2023 11:07
-
-
Save Aadithya-V/633716256d37c0cc3342f3cdea3551f9 to your computer and use it in GitHub Desktop.
FloorMod for Go
This file contains 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
// For modulo not remainder: | |
// n mod m = n-⌊nm⌋.m | |
func FloorMod(n, m int) int { | |
res := n % m // remainder operator, strictly speaking: example- | |
//If -8 % 7 == 6, you're fine, but if it is -1, you'll need to adjust it by | |
//adding m to any negative results as below. | |
if res < 0 { | |
res += m | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment