module Lib ( factors, fib, primes, primeFactors, sumOfDigits, ) where import Data.List (nub) factors :: Integral a => a -> [a] factors n = concat [if mod n f == 0 then nub [f, div n f] else [] | f <- [1 .. flsqrt n]] fib :: [Integer] fib = 1 : 2 : zipWith (+) fib (tail fib) flsqrt :: Integral a => a -> a flsqrt n = floor $ sqrt $ fromIntegral n isPrime :: Integral a => a -> Bool isPrime n = n > 1 && null [[] | k <- [2 .. flsqrt n], mod n k == 0] primes :: [Integer] primes = 2 : filter isPrime [3, 4 ..] primeFactors :: Integral a => a -> [a] primeFactors n = genPrimeFactors n 2 where genPrimeFactors :: Integral a => a -> a -> [a] genPrimeFactors n c | c * c > n = [n] | mod n c /= 0 = genPrimeFactors n (c + 1) | otherwise = c : genPrimeFactors (div n c) c sumOfDigits :: Integral a => a -> a sumOfDigits n | n == 0 = 0 | otherwise = mod n 10 + sumOfDigits (div n 10)