Jump to content

Lah numbers: Difference between revisions

m
→‎{{header|Haskell}}: An applicative pruning of some minor redundancy.
m (→‎{{header|Haskell}}: An applicative pruning of some minor redundancy.)
Line 467:
<lang haskell>import Text.Printf (printf)
import Control.Monad (when)
import Control.Applicative (liftA2)
 
factorial :: Integral n => n -> n
lah :: Integral n => n -> n -> n
=> n -> n
factorial 0 = 1
factorial n = product [1 .. n]
 
lah
lah :: Integral n => n -> n -> n
:: Integral n
=> n -> n -> n
lah n k
| k == 1 = factorial n
| k == n = 1
| k > n = 0
| k < 1 || n < 1 = 0
| otherwise = (factorialf n *`div` factorialf (pred n))k `div` factorial (n - k)
where
(factorial k * factorial (pred k)) `div`
f = (*) . factorial <*> (factorial (n -. kpred)
 
printLah :: (Word, Word) -> IO ()
Line 492 ⟶ 497:
mapM_ (printf "%11d") zeroToTwelve
mapM_ printLah $ (,) <$> zeroToTwelve <*> zeroToTwelve
printf
printf "\nMaximum value from the L(100, *) row:\n%d\n"
(maximum"\nMaximum $value lahfrom 100 <$>the L([0..100], *) row:: [Integer]))\n%d\n"
(maximum $ lah 100 <$> ([0 .. 100] :: [Integer]))
where zeroToTwelve = [0..12]</lang>
where
where zeroToTwelve = [0 .. 12]</lang>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.