module Sol_Exercise_1 where import Test.QuickCheck import Data.List {- G1.1 -} threeDifferent :: Integer -> Integer -> Integer -> Bool threeDifferent x y z = x /= y && x /= z && y /= z fourEqual :: Integer -> Integer -> Integer -> Integer -> Bool fourEqual w x y z = w == x && x == y && y == z {- - threeDifferent (2 + 3) 5 (11 `div` 2) - = (2 + 3) /= 5 && (2 + 3) /= (11 `div` 2) && 5 /= (11 `div` 2) - = 5 /= 5 && 5 /= (11 `div` 2) && 5 /= (11 `div` 2) - = False && 5 /= (11 `div` 2) && 5 /= (11 `div` 2) - = False - - fourEqual (2 + 3) 5 (11 `div` 2) (21 `mod` 11) - = (2 + 3) == 5 && 5 == (11 `div` 2) && (11 `div 2) == (21 `mod` 11) - = 5 == 5 && 5 == (11 `div` 2) && (11 `div 2) == (21 `mod` 11) - = True && 5 == (11 `div` 2) && (11 `div 2) == (21 `mod` 11) - = 5 == (11 `div` 2) && (11 `div 2) == (21 `mod` 11) - = 5 == 5 && 5 == (21 `mod` 11) - = True && 5 == (21 `mod` 11) - = 5 == (21 `mod` 11) - = 5 == 10 - = False -} {- G1.2 -} fac :: Integer -> Integer fac n | n > 0 = n * fac (n - 1) | otherwise = 1 sum_ten :: Integer -> Integer sum_ten n = sum_ten' n 9 sum_ten' :: Integer -> Integer -> Integer sum_ten' n 0 = n sum_ten' n m = n + m + sum_ten' n (m - 1) {- G1.3 -} g :: Integer -> Integer g n = if n < 10 then n*n else n max_g :: Integer -> Integer max_g 0 = 0 max_g n | n > 0 = if (g mg > g n) then mg else n | otherwise = 0 where mg = max_g (n - 1) max_g' :: Integer -> Integer max_g' n | n < 0 = 0 | n >= 10 && n < 81 = 9 | otherwise = n prop_max_g n = max_g n == max_g' n {- H... -} sum_max_sq :: Integer -> Integer -> Integer -> Integer sum_max_sq x y z | x >= y && y >= z = x * x + y * y | x >= z && z >= y = x * x + z * z | y >= x && x >= z = y * y + x * x | y >= z && z >= x = y * y + z * z | z >= x && x >= y = z * z + x * x | z >= y && y >= x = z * z + y * y {-WETT-} sum_max_sq' :: Integer -> Integer -> Integer -> Integer sum_max_sq' x y z = x ^ 2 + y ^ 2 + z ^ 2 - x `min` y `min` z ^ 2 {-TTEW-} {-WETT-} sum_max_sq'' :: Integer -> Integer -> Integer -> Integer sum_max_sq'' x y z = sum $ tail $ (^2) `map` sort [x, y, z] {-TTEW-} -- sum_max_sq_test1 = quickCheck (\x y z -> sum_max_sq x y z == sum_max_sq_alt x y z) f :: Integer -> Integer f n | n > 100 = n - 10 | otherwise = f (f (n + 11)) f' :: Integer -> Integer f' n = if n > 101 then n - 10 else 91 f'_wrong :: Integer -> Integer f'_wrong n = 91 -- f_test1 = quickCheckWith stdArgs { maxSize = 1000 } (\n -> f n == f' n) -- f_test2 = quickCheckWith stdArgs { maxSize = 1000 } (\n -> f n == f'_wrong n) is_square' :: Integer -> Integer -> Bool is_square' n 0 = (n == 0) is_square' n m | m > 0 = if n == m*m then True else is_square' n (m - 1) | otherwise = False {- is_square'_test1 = quickCheck (\n m -> test n m == is_square' n m) where test :: Integer -> Integer -> Bool test n m | n >= 0 = let s = sqrt (fromInteger n) in s == fromIntegral (floor s) && s <= fromInteger m | otherwise = False -} is_square :: Integer -> Bool is_square n = is_square' n n {- is_square_test1 = quickCheck (\n -> test n == is_square n) where test :: Integer -> Bool test n | n >= 0 = let s :: Float s = sqrt (fromInteger n) in s == fromIntegral (floor s) | otherwise = False -}