How to resolve the algorithm Lah numbers step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Lah numbers step by step in the Prolog 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 Prolog programming language
Source code in the prolog programming language
% Reference: https://en.wikipedia.org/wiki/Lah_number#Identities_and_relations
:- dynamic unsigned_lah_number_cache/3.
unsigned_lah_number(N, N, 1):-!.
unsigned_lah_number(_, 0, 0):-!.
unsigned_lah_number(N, K, 0):-
K > N,
!.
unsigned_lah_number(N, K, L):-
unsigned_lah_number_cache(N, K, L),
!.
unsigned_lah_number(N, K, L):-
N1 is N - 1,
K1 is K - 1,
unsigned_lah_number(N1, K, L1),
unsigned_lah_number(N1, K1, L2),
!,
L is (N1 + K) * L1 + L2,
assertz(unsigned_lah_number_cache(N, K, L)).
print_unsigned_lah_numbers(N):-
between(1, N, K),
unsigned_lah_number(N, K, L),
writef('%11r', [L]),
fail.
print_unsigned_lah_numbers(_):-
nl.
print_unsigned_lah_numbers:-
between(1, 12, N),
print_unsigned_lah_numbers(N),
fail.
print_unsigned_lah_numbers.
max_unsigned_lah_number(N, Max):-
aggregate_all(max(L), (between(1, N, K), unsigned_lah_number(N, K, L)), Max).
main:-
writeln('Unsigned Lah numbers up to L(12,12):'),
print_unsigned_lah_numbers,
writeln('Maximum value of L(n,k) where n = 100:'),
max_unsigned_lah_number(100, M),
writeln(M).
You may also check:How to resolve the algorithm Count in factors step by step in the Ring programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the REXX programming language
You may also check:How to resolve the algorithm A+B step by step in the Scratch programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the RPL programming language