Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Lah-numbers/Haskell/lah-numbers.hs
Normal file
30
Task/Lah-numbers/Haskell/lah-numbers.hs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import Text.Printf (printf)
|
||||
import Control.Monad (when)
|
||||
|
||||
factorial :: Integral n => n -> n
|
||||
factorial 0 = 1
|
||||
factorial n = product [1..n]
|
||||
|
||||
lah :: Integral n => n -> n -> n
|
||||
lah n k
|
||||
| k == 1 = factorial n
|
||||
| k == n = 1
|
||||
| k > n = 0
|
||||
| k < 1 || n < 1 = 0
|
||||
| otherwise = f n `div` f k `div` factorial (n - k)
|
||||
where
|
||||
f = (*) =<< (^ 2) . factorial . pred
|
||||
|
||||
printLah :: (Word, Word) -> IO ()
|
||||
printLah (n, k) = do
|
||||
when (k == 0) (printf "\n%3d" n)
|
||||
printf "%11d" (lah n k)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
printf "Unsigned Lah numbers: L(n, k):\nn/k"
|
||||
mapM_ (printf "%11d") zeroToTwelve
|
||||
mapM_ printLah $ (,) <$> zeroToTwelve <*> zeroToTwelve
|
||||
printf "\nMaximum value from the L(100, *) row:\n%d\n"
|
||||
(maximum $ lah 100 <$> ([0..100] :: [Integer]))
|
||||
where zeroToTwelve = [0..12]
|
||||
Loading…
Add table
Add a link
Reference in a new issue