Skip to content

Instantly share code, notes, and snippets.

@rhaseven7h
Created July 28, 2016 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhaseven7h/30a0cb7684ffc2b1b6d75eaaf9d706a9 to your computer and use it in GitHub Desktop.
Save rhaseven7h/30a0cb7684ffc2b1b6d75eaaf9d706a9 to your computer and use it in GitHub Desktop.
HackerRank.com - Functional Programming - Haskell - Super Digit

Super Digit

We define super digit of an integer x using the following rules:

  • If x has only 1 digit, then its super digit is x.
  • Otherwise, the super digit of x is equal to the super digit of the digit-sum of x. Here, digit-sum of a number is defined as the sum of its digits.

For example, super digit of 9875 will be calculated as:

super-digit(9875) = super-digit(9+8+7+5) 
                  = super-digit(29) 
                  = super-digit(2+9)
                  = super-digit(11)
                  = super-digit(1+1)
                  = super-digit(2)
                  = 2.

You are given two numbers - n and k. You have to calculate the super digit of P.

P is created when number n is concatenated k times. That is, if n = 123 and k = 3, then P = 123123123.

Input Format

Input will contain two space separated integers, n and k.

Output Format

Output the super digit of P, where P is created as described above.

Constraint

  • 1 <= n <= 10^100,000
  • 1 <= k <= 10^5

Sample Input

148 3

Sample Output

3

Explanation

Here n = 148 and k = 3, so P = 148148148.

super-digit(P) = super-digit(148148148) 
               = super-digit(1+4+8+1+4+8+1+4+8)
               = super-digit(39)
               = super-digit(3+9)
               = super-digit(12)
               = super-digit(1+2)
               = super-digit(3)
               = 3.
sumOfDigits :: [Char] -> [Char]
sumOfDigits num = show . sum $ map (\c -> read [c] :: Int) num
getSums :: [[Char]] -> [Char] -> [[Char]]
getSums lst [c] = [c] : lst
getSums lst num = getSums (num : lst) (sumOfDigits num)
solve :: Int -> [Char] -> [[Char]]
solve 1 [c] = [[c]]
solve k n = getSums [] snm
where
sod = sumOfDigits n
sdn = read sod :: Int
nnm = k * sdn
snm = show nnm
main = do
raw <- getContents
let [n, k] = words $ head $ lines raw
putStrLn . head $ solve (read k :: Int) n
7404954009694227446246375747227852213692570890717884174001587537145838723390362624487926131161112710589127423098959327020544003395792482625191721603328307774998124389641069884634086849138515079220750462317357487762780480576640689175346956135668451835480490089962406773267569650663927778867764315211280625033388271518264961090111547480467065229843613873499846390257375933040086863430523668050046930387013897062106309406874425001127890574986610018093859693455518413268914361859000614904461902442822577552997680098389183082654625098817411306985010658756762152160904278169491634807464356130877526392725432086439934006728914411061861235300979536190100734360684054557448454640750198466877185875290011114667186730452681943043971812380628117527172389889545776779555664826488520325234792648448625225364535053605515386730925070072896004645416713682004600636574389040662827182696337187610904694029221880801372864040345567230941110986028568372710970460116491983700312243090679537497139499778923997433720159174153 100000
@kishlaya
Copy link

kishlaya commented Sep 28, 2019

Here's another slick way of doing it:

main = do
    ip <- getLine
    putStrLn $ g (words ip)

g :: [String] -> String
g [n,k] = show (solve (read n) (read k))

solve :: Integral a => a -> a -> a
solve n k
    | x == 0 = 9
    | otherwise = x
    where x = mod (n*k) 9

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