-- Ex1 mymin :: Int -> Int -> Int mymin a b = if a < b then a else b -- Ex2 isInside :: Int -> Int -> Int -> Bool isInside x a b = a <= x && x <= b -- Ex3 myfunc :: Double -> Double -> Double myfunc a b = ((sqrt a) + (sqrt b)) / 2 -- Ex4 myfib :: Int -> Int myfib n = if n == 1 then 0 else if n == 2 then 1 else myfib (n - 1) + myfib (n - 2) -- Ex5 mygcd :: Int -> Int -> Int mygcd a b = if b == 0 then a else mygcd b (mod a b) -- Ex6 mymindivisor :: Int -> Int -> Int mymindivisor x start = if (rem x start) == 0 then start else mymindivisor x (start + 1) mymaxdivisor :: Int -> Double mymaxdivisor x = (fromIntegral x) / (fromIntegral (mymindivisor x 2)) -- Ex7 sumOdd :: Int -> Int -> Int sumOdd a b = if a > b then 0 else if (mod a 2) == 0 then a + 1 + (sumOdd (a + 3) b) else a + (sumOdd (a + 2) b) -- Ex8 isPrime :: Int -> Bool isPrime x | x <= 3 = True | (mod x 2) == 0 || (mod x 3) == 0 = False | otherwise = calc 6 x (sqrt (fromIntegral x)) where calc :: Int -> Int -> Double -> Bool calc i x max | (fromIntegral (i - 1)) > max = True | (mod x (i - 1)) == 0 || (mod x (i + 1)) == 0 = False | otherwise = calc (i + 6) x max -- Ex9 numLenT10 :: Int -> Int numLenT10 x = calc x 1 where calc :: Int -> Int -> Int calc x res | x == 0 = res | otherwise = calc (div x 10) (res * 10) isPalindrome :: Int -> Bool isPalindrome x = calc x (div (numLenT10 x) 10) where calc :: Int -> Int -> Bool calc x xT10 | (div x 10) == 0 = True | (rem x 10) /= (div x xT10) = False | otherwise = calc (rem (div x 10) xT10) (div xT10 10) countPalindromes :: Int -> Int -> Int countPalindromes a b = calc a b 0 where calc :: Int -> Int -> Int -> Int calc a b sum | a > b = sum | isPalindrome a = calc (a + 1) b (sum + 1) | otherwise = calc (a + 1) b sum -- Ex10 sumDivisors :: Int -> Int sumDivisors x = calc x 0 1 (div x 2) where calc :: Int -> Int -> Int -> Int -> Int calc x sum i max | i > max = sum | (rem x i) == 0 = calc x (sum + i) (i + 1) max | otherwise = calc x sum (i + 1) max main :: IO () main = do print (mymin 5 6) -- 5 print (isInside 7 (-48) 299) -- True print (myfunc 25 49) -- 6 print (myfib 8) -- 13 print (mygcd 48 18) -- 6 print (mymaxdivisor 2145) -- 715 print (sumOdd 2 6) -- 8 print (isPrime 6367) -- True print (countPalindromes 1 100) -- 18 print (sumDivisors 1800) -- 4245