Created
June 28, 2011 03:52
-
-
Save seri/1050451 to your computer and use it in GitHub Desktop.
Haskell solution to TopCoder's Lucky Remainder problem
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
import Data.Char | |
pow2mod9 :: Int -> Int | |
pow2mod9 0 = 1 | |
pow2mod9 1 = 2 | |
pow2mod9 2 = 4 | |
pow2mod9 3 = 8 | |
pow2mod9 4 = 7 | |
pow2mod9 5 = 5 | |
pow2mod9 n = pow2mod9 $ mod n 6 | |
luckyRemainder :: [Int] -> Int | |
luckyRemainder ds = mod (mod (sum ds) 9 * pow2mod9 (length ds - 1)) 9 | |
getLuckyRemainder :: String -> Int | |
getLuckyRemainder = luckyRemainder . map toDigit | |
where toDigit c = ord c - ord '0' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Link to problem statement