Skip to content

Instantly share code, notes, and snippets.

@seri
Created June 28, 2011 03:52
Show Gist options
  • Save seri/1050451 to your computer and use it in GitHub Desktop.
Save seri/1050451 to your computer and use it in GitHub Desktop.
Haskell solution to TopCoder's Lucky Remainder problem
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'
@seri
Copy link
Author

seri commented Jun 28, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment