module Numeric.Pure (
derangement
, hsIsPrime
, hsFibonacci
) where
#if __GLASGOW_HASKELL__ <= 784
import Control.Applicative
#endif
derangement :: (Integral a) => Int -> a
derangement n = derangements !! n
derangements :: (Integral a) => [a]
derangements = fmap snd g
where g = (0, 1) : (1, 0) : zipWith (\(_, n) (i, m) -> (i + 1, i * (n + m))) g (tail g)
hsIsPrime :: (Integral a) => a -> Bool
hsIsPrime 1 = False
hsIsPrime x = all ((/=0) . (x `mod`)) [2..m]
where m = floor (sqrt (fromIntegral x :: Float))
fibs :: (Integral a) => [a]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
hsFibonacci :: (Integral a) => Int -> a
hsFibonacci = (fibs !!)