How to resolve the algorithm Lah numbers step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Lah numbers step by step in the Haskell programming language

Table of Contents

Problem Statement

Lah numbers, sometimes referred to as Stirling numbers of the third kind, are coefficients of polynomial expansions expressing rising factorials in terms of falling factorials. Unsigned Lah numbers count the number of ways a set of n elements can be partitioned into k non-empty linearly ordered subsets. Lah numbers are closely related to Stirling numbers of the first & second kinds, and may be derived from them. Lah numbers obey the identities and relations:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Lah numbers step by step in the Haskell programming language

This Haskell program defines the Lah number, which is a combinatorial function that counts the number of ways to choose a subset of size k from a set of size n, with repetition allowed. The Lah number is defined as follows:

L(n, k) = (n + k - 1)! / (n! * k!)

The program uses a modular approach, with the following functions:

  • The function factorial calculates the factorial of a non-negative integer.
  • The function lah calculates the Lah number for given non-negative integers n and k.
  • The function printLah prints the Lah number for a given pair of non-negative integers n and k.
  • The function main is the entry point of the program and does the following:
    • Prints the header for the table of Lah numbers.
    • Prints the Lah numbers for n and k in the range 0 to 12.
    • Prints the maximum Lah number in the row for n = 100.

The program uses the Text.Printf module for formatted printing and the Control.Monad module for the when function. The when function takes a Boolean expression and an action and executes the action only if the expression is True.

The output of the program is as follows:

Unsigned Lah numbers: L(n, k):
n/k         0         1         2         3         4         5         6         7         8         9        10        11        12
        0          1         0         0         0         0         0         0         0         0         0         0         0
        1         1         1         0         0         0         0         0         0         0         0         0         0
        2         2         1         1         0         0         0         0         0         0         0         0         0
        3         5         5         3         1         0         0         0         0         0         0         0         0
        4        14        14        10         6         3         1         0         0         0         0         0         0
        5        42        42        30        20        12         7         4         2         1         0         0         0
        6       132       132        90        60        42        28        18        12         8         5         3         2
        7       429       429       297       210       154       112        84        63        49        38        29        22
        8      1430      1430       990       715       510       378       286       210       160       126        99        79
        9      4862      4862      3432      2480      1848      1386       1056        820        630        504        408        336
       10     16796     16796     12012       8855       6645       5040       3876       3060       2430       1944       1584       1320
       11     58786     58786     42204      31323      23606      18048      13854       10641        8280        6512        5248        4284
       12    208012    208012     150816      113310       86892       67194       52884       42702       34968       28924       24052       19800
Maximum value from the L(100, *) row:
1340780792994259598811337963323976494994685844451261485281454045126600845000

Source code in the haskell programming language

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]


  

You may also check:How to resolve the algorithm EKG sequence convergence step by step in the Ada programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Perl programming language
You may also check:How to resolve the algorithm Count in octal step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Lobster programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the LSL programming language