diff --git a/74 b/74 index 8efd556..47fbf37 100755 Binary files a/74 and b/74 differ diff --git a/74.hs b/74.hs index dda4ba7..1980a4d 100644 --- a/74.hs +++ b/74.hs @@ -18,7 +18,7 @@ type ChainMap = Map.Map Integer Integer initialMap :: ChainMap initialMap = Map.fromList [(169, 3), (363601, 3), (1454, 3), (871, 2), (872, 2), (45361, 2), (45362, 2), - (1, 1), (2, 1), (145, 1)] + (1, 1), (2, 1), (145, 1), (40585, 1)] sumOfFactorialOfDigits :: Integer -> Integer sumOfFactorialOfDigits n = sum $ map (digitFactorial . fromIntegral . digitToInt) $ show n @@ -35,5 +35,5 @@ numOfLoops :: Integer -> Integer -> (Integer, ChainMap) numOfLoops bound length = foldr (\n (num, oldMap) -> let (chainLength, newMap) = getChainLength n oldMap in (num + if chainLength == length then 1 else 0, newMap)) (0, initialMap) [3..bound] -main = print $ let (length, sizeMap) = numOfLoops 10000 60 +main = print $ let (length, sizeMap) = numOfLoops 1000000 60 in length diff --git a/74.o b/74.o index 3ddbde1..ca90bba 100644 Binary files a/74.o and b/74.o differ diff --git a/74_alt b/74_alt new file mode 100755 index 0000000..9e5c413 Binary files /dev/null and b/74_alt differ diff --git a/74_alt.hi b/74_alt.hi new file mode 100644 index 0000000..9ebf58d Binary files /dev/null and b/74_alt.hi differ diff --git a/74_alt.hs b/74_alt.hs new file mode 100644 index 0000000..4f20f8f --- /dev/null +++ b/74_alt.hs @@ -0,0 +1,39 @@ +import Data.Char (digitToInt) + +digitFactorial :: Integer -> Integer +digitFactorial n = case n of + 0 -> 1 + 1 -> 1 + 2 -> 2 + 3 -> 6 + 4 -> 24 + 5 -> 120 + 6 -> 720 + 7 -> 5040 + 8 -> 40320 + 9 -> 362880 + +loops :: Integer -> Maybe Integer +loops n + | n `elem` [169, 363601, 1454] = Just 3 + | n `elem` [871, 872, 45361, 45362] = Just 2 + | n `elem` [1, 2, 145, 40585] = Just 1 + | otherwise = Nothing + +sumOfFactorialOfDigits :: Integer -> Integer +sumOfFactorialOfDigits n = sum $ map (digitFactorial . fromIntegral . digitToInt) $ show n + +getChainLength :: Integer -> Integer +getChainLength n = getChainLengthRec n 1 + +getChainLengthRec :: Integer -> Integer -> Integer +getChainLengthRec n length = + let next = sumOfFactorialOfDigits n + in case loops next of + Just x -> length + x + Nothing -> getChainLengthRec next (length + 1) + +numOfLoops :: Integer -> Integer -> Integer +numOfLoops bound length = foldr (\n num -> num + if getChainLength n == length then 1 else 0) 0 [3..bound] + +main = print $ numOfLoops 1000000 60 diff --git a/74_alt.o b/74_alt.o new file mode 100644 index 0000000..6223fe4 Binary files /dev/null and b/74_alt.o differ