Saturday, May 30, 2020

Recursive Digit Sum - Hacker Rank Solution

Recursive Digit Sum - Hacker Rank Solution
import Data.List
import Data.Char

main :: IO ()
main = getContents >>= print. (\[n, k] -> superDigit ((read k) * (getSumStr n))). words

superDigit n
  | n < 10 = n
  | otherwise = superDigit (getSum n)

getSum 0 = 0
getSum n = n`rem`10 + getSum (n`div`10)

getSumStr [] = 0
getSumStr (x:xs) = (ord x - ord '0') + getSumStr xs

No comments:

Post a Comment

Powered by Blogger.