Skip to content

Instantly share code, notes, and snippets.

@arvind-iyer
Last active June 22, 2016 15:34
Show Gist options
  • Save arvind-iyer/c1a3f01e950c5e0e9c955b1511e3cc70 to your computer and use it in GitHub Desktop.
Save arvind-iyer/c1a3f01e950c5e0e9c955b1511e3cc70 to your computer and use it in GitHub Desktop.
Find the last digit on sequentially exponentiating a list of Integer values from left to right. E.g. [3,4,5] => (3 ^ ( 4 ^ 5 )) mod 10
module Kata
(
lastDigitOnExp
,lastDigitListReduce
) where
lookupTable = [ [0,0,0,0],
[1,1,1,1],
[2,4,8,6],
[3,9,7,1],
[4,6,4,6],
[5,5,5,5],
[6,6,6,6],
[7,9,3,1],
[8,4,2,6],
[9,1,9,1] ]
lastDigitOnExp :: Int -> Int -> Int
lastDigitOnExp base exp = lookupTable !! mod base 10 !! mod (exp-1) 4
lastDigitListReduce :: [Int] -> Int
lastDigitListReduce x = foldr1 lastDigitOnExp x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment