- Commit
- b9966258b957c6786c86e734269649a7bec4ec35
- Parent
- 903a387be828f9ad7e90c06bbdf33ddab14d84fe
- Author
- Pablo Emilio Escobar Gaviria <pablo-escobar@riseup.net>
- Date
Update Main.hs
An exercise on polyglossy: the same problem solved on multiple languages
Update Main.hs
1 file changed, 54 insertions, 54 deletions
Status | File Name | N° Changes | Insertions | Deletions |
Modified | haskell/Main.hs | 108 | 54 | 54 |
diff --git a/haskell/Main.hs b/haskell/Main.hs @@ -1,54 +1,54 @@ --- The following program is a simple test for the following conjecture: - --- Let S: N -> N be the sum of the digits of a positive integer. --- For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer. - -module Main where - -import Numeric (readDec) -import Numeric.Natural -import System.Environment -import System.Exit -import Control.Monad (foldM) -import Data.Vector (Vector, unsafeIndex, generate) - -main :: IO Int -main = do - args <- getArgs - - case readDec <$> head' args of - Just [(max, "")] -> - if counterexempl max then exitFailure else exitSuccess - - _ -> exitInvalidInput - - where head' [] = Nothing - head' xs = Just (head xs) - --- Calculates the sum of the digits of `n`. -sumDigits :: Int -> Int -sumDigits = sumDigitsTail 0 - where sumDigitsTail acc n - | n == 0 = acc - | otherwise = sumDigitsTail (acc + (n `mod` 10)) (n `div` 10) - --- Returns `True` if the if the conjecture holds for pair. --- Otherwise returns `False`. -test' :: Vector Int -> (Int, Int) -> Bool -test' sums (a, b) = diff `mod` 9 == 0 - where diff = sums !! (a + b) - sums !! a - sums !! b - (!!) = unsafeIndex - --- Checks if there is any counterexample in --- [(a, b) | a <- [0..max], b <- [a..max]]. --- --- Returns `True` if a counter example was found. --- Otherwise returns `False`. -counterexempl :: Int -> Bool -counterexempl max = - all (test' sums) [(a, b) | a <- [0..max], b <- [a..max]] - where sums = generate (2 * max + 1) sumDigits - -exitInvalidInput :: IO Int -exitInvalidInput = exitWith $ ExitFailure 2 - +-- The following program is a simple test for the following conjecture: + +-- Let S: N -> N be the sum of the digits of a positive integer. +-- For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer. + +module Main where + +import Numeric (readDec) +import Numeric.Natural +import System.Environment +import System.Exit +import Control.Monad (foldM) +import Data.Vector (Vector, unsafeIndex, generate) + +main :: IO Int +main = do + args <- getArgs + + case readDec <$> head' args of + Just [(max, "")] -> + if counterexempl max then exitFailure else exitSuccess + + _ -> exitInvalidInput + + where head' [] = Nothing + head' xs = Just (head xs) + +-- Calculates the sum of the digits of `n`. +sumDigits :: Int -> Int +sumDigits = sumDigitsTail 0 + where sumDigitsTail acc n + | n == 0 = acc + | otherwise = sumDigitsTail (acc + (n `mod` 10)) (n `div` 10) + +-- Returns `True` if the if the conjecture holds for the pair. +-- Otherwise returns `False`. +test' :: Vector Int -> (Int, Int) -> Bool +test' sums (a, b) = diff `mod` 9 == 0 + where diff = sums !! (a + b) - sums !! a - sums !! b + (!!) = unsafeIndex + +-- Checks if there is any counterexample in +-- [(a, b) | a <- [0..max], b <- [a..max]]. +-- +-- Returns `True` if a counter example was found. +-- Otherwise returns `False`. +counterexempl :: Int -> Bool +counterexempl max = + all (test' sums) [(a, b) | a <- [0..max], b <- [a..max]] + where sums = generate (2 * max + 1) sumDigits + +exitInvalidInput :: IO Int +exitInvalidInput = exitWith $ ExitFailure 2 +