Lah numbers: Difference between revisions

54,652 bytes added ,  2 months ago
added RPL
(→‎{{header|VBScript}}: Section added)
(added RPL)
 
(41 intermediate revisions by 24 users not shown)
Line 36:
 
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F lah(BigInt n, BigInt k)
I k == 1
R factorial(n)
I k == n
R BigInt(1)
I k > n
R BigInt(0)
I k < 1 | n < 1
R BigInt(0)
R (factorial(n) * factorial(n - 1)) I/ (factorial(k) * factorial(k - 1)) I/ factorial(n - k)
 
print(‘Unsigned Lah numbers: L(n, k):’)
print(‘n/k ’, end' ‘ ’)
L(i) 13
print(‘#11’.format(i), end' ‘ ’)
print()
L(row) 13
print(‘#<4’.format(row), end' ‘ ’)
L(i) 0 .. row
V l = lah(row, i)
print(‘#11’.format(l), end' ‘ ’)
print()
print("\nMaximum value from the L(100, *) row:")
V maxVal = max((0.<100).map(a -> lah(100, a)))
print(maxVal)</syntaxhighlight>
 
{{out}}
<pre>
Unsigned Lah numbers: L(n, k):
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 2 1
3 0 6 6 1
4 0 24 36 12 1
5 0 120 240 120 20 1
6 0 720 1800 1200 300 30 1
7 0 5040 15120 12600 4200 630 42 1
8 0 40320 141120 141120 58800 11760 1176 56 1
9 0 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
 
Maximum value from the L(100, *) row:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN fac n:
PUT 1 IN result
FOR i IN {1..n}: PUT result*i IN result
RETURN result
 
HOW TO RETURN n lah k:
SELECT:
n=k: RETURN 1
n=0 OR k=0: RETURN 0
k=1: RETURN fac n
ELSE: RETURN (((fac n)*fac(n-1)) / ((fac k)*fac(k-1)) )/ fac(n-k)
 
HOW TO SHOW LAH TABLE UP TO N nmax:
FOR n IN {0..nmax}:
FOR k IN {0..n}:
WRITE (n lah k)>>11
WRITE /
 
HOW TO RETURN max.lah.number n:
PUT 0 IN max
FOR k IN {0..n}:
PUT n lah k IN cur
IF cur>max: PUT cur IN max
RETURN max
 
 
SHOW LAH TABLE UP TO N 12
WRITE /
WRITE "Maximum value where n=100:"/
WRITE max.lah.number 100/</syntaxhighlight>
{{out}}
<pre> 0 1
0 2 1
0 6 6 1
0 24 36 12 1
0 120 240 120 20 1
0 720 1800 1200 300 30 1
0 5040 15120 12600 4200 630 42 1
0 40320 141120 141120 58800 11760 1176 56 1
0 362880 1451520 1693440 846720 211680 28224 2016 72 1
0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
 
Maximum value where n=100:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{Trans|ALGOL W}}
Uses Algol 68G's LONG LONG INT which has programmer-specifiable precision, so can find Lah numbers with n = 100 and need not use the scaling "trick" used by the Algol W sample.
<syntaxhighlight lang="algol68">BEGIN # calculate Lah numbers upto L( 12, 12 ) #
PR precision 400 PR # set the precision for LONG LONG INT #
# returns Lah Number L( n, k ), f must be a table of factorials to at least n #
PROC lah = ( INT n, k, []LONG LONG INT f )LONG LONG INT:
IF n = k THEN 1
ELIF n = 0 OR k = 0 THEN 0
ELIF k = 1 THEN f[ n ]
ELIF k > n THEN 0
ELSE
# general case: ( n! * ( n - 1 )! ) / ( k! * ( k - 1 )! ) / ( n - k )! #
# we re-arrange the above to: #
# ( n! / k! ) -- t1 #
# * ( ( n - 1 )! / ( k - 1 )! ) -- t2 #
# / ( n - k )! #
LONG LONG INT t1 = f[ n ] OVER f[ k ];
LONG LONG INT t2 = f[ n - 1 ] OVER f[ k - 1 ];
( t1 * t2 ) OVER f[ n - k ]
FI # lah # ;
INT max n = 100; # max n for Lah Numbers #
INT max display = 12; # max n to display L( n, k ) values #
# table of factorials up to max n #
[ 1 : max n ]LONG LONG INT factorial;
BEGIN
LONG LONG INT f := 1;
FOR i TO UPB factorial DO factorial[ i ] := f *:= i OD
END;
# show the Lah numbers #
print( ( "Unsigned Lah numbers", newline ) );
print( ( "n/k 0" ) );
FOR i FROM 1 TO max display DO print( ( whole( i, -11 ) ) ) OD;
print( ( newline ) );
FOR n FROM 0 TO max display DO
print( ( whole( n, -2 ) ) );
print( ( whole( lah( n, 0, factorial ), -4 ) ) );
FOR k FROM 1 TO n DO
print( ( whole( lah( n, k, factorial ), -11 ) ) )
OD;
print( ( newline ) )
OD;
# maximum value of a Lah number for n = 100 #
LONG LONG INT max 100 := 0;
FOR k FROM 0 TO 100 DO
LONG LONG INT lah n k = lah( 100, k, factorial );
IF lah n k > max 100 THEN max 100 := lah n k FI
OD;
print( ( "maximum Lah number for n = 100: ", whole( max 100, 0 ), newline ) )
END</syntaxhighlight>
{{out}}
<pre>
Unsigned Lah numbers
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 2 1
3 0 6 6 1
4 0 24 36 12 1
5 0 120 240 120 20 1
6 0 720 1800 1200 300 30 1
7 0 5040 15120 12600 4200 630 42 1
8 0 40320 141120 141120 58800 11760 1176 56 1
9 0 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
maximum Lah number for n = 100: 44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|ALGOL W}}==
Algol W's only integer type is signed 32 bit integers.
<br>
As L(12,2), L(12,3) and L(12,4) are too large for signed 32 bit integers, this sample scales the result by an appropriate power of 10 to enable the table to be printed up to L(12,12). Luckily, the problematic L(12,k) values all have at least 2 trailing zeros.
<syntaxhighlight lang="algolw">begin % calculate Lah numbers upto L( 12, 12 ) %
% sets lahNumber to L( n, k ), lahScale is returned as the power of 10 %
% lahNumber should be multiplied by %
% f must be a table of factorials to at least n %
procedure L ( integer value n, k
; integer array f ( * )
; integer result lahNumber, lahScale
) ;
if n = k then begin lahNumber := 1; lahScale := 0 end
else if n = 0 or k = 0 then begin lahNumber := 0; lahScale := 0 end
else if k = 1 then begin lahNumber := f( n ); lahScale := 0 end
else if k > n then begin lahNumber := 0; lahScale := 0 end
else begin
% general case: ( n! * ( n - 1 )! ) / ( k! * ( k - 1 )! ) / ( n - k )! %
% Algol W has only 32 bit signed integers so we need to avoid overflow %
% we re-arrange the above to: %
% ( n! / k! / ( n - k ) ! ) -- t1 %
% * ( ( n - 1 )! / ( k - 1 )! -- t2 %
% and if necessary, reduce t1 and t2 by powers of 10 %
integer t1, t2, d1, d2, v;
t1 := f( n ) div f( k ) div f( n - k );
t2 := f( n - 1 ) div f( k - 1 );
% calculate the number of digits for t1 and t2 %
lahScale := d1 := d2 := 0;
v := t1; while v > 0 do begin d1 := d1 + 1; v := v div 10 end;
v := t2; while v > 0 do begin d2 := d2 + 1; v := v div 10 end;
if d1 + d2 > 8 then begin
% the result will overflow reduce t1 and t2 by an appropriate power %
% of 10 and set lahScale accordingly %
while t1 rem 10 = 0 do begin lahScale := lahScale + 1; t1 := t1 div 10 end;
while t2 rem 10 = 0 do begin lahScale := lahScale + 1; t2 := t2 div 10 end;
end if_d1_plus_d2_gt_8;
lahNumber := t1 * t2
end L;
% table of factorials up to 12 %
integer array factorial ( 1 :: 12 );
% compute the factorials %
begin
integer f; f := 1;
for i := 1 until 12 do begin f := f * i; factorial( i ) := f end
end;
% show the Lah numbers %
write( "Unsigned Lah numbers" );
write( "n/k " );
for i := 0 until 12 do writeon( i_w := 11, s_w := 0, i );
for n := 0 until 12 do begin
write( s_w := 2, i_w := 2, n );
for k := 0 until n do begin
integer lahNumber, lahScale;
L( n, k, factorial, lahNumber, lahScale );
writeon( S_W := 0, i_w := 11 - lahScale, lahNumber );
for s := 1 until lahScale do writeon( s_w := 0, "0" )
end for_k
end for_n
end.</syntaxhighlight>
{{out}}
<pre>
Unsigned Lah numbers
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 2 1
3 0 6 6 1
4 0 24 36 12 1
5 0 120 240 120 20 1
6 0 720 1800 1200 300 30 1
7 0 5040 15120 12600 4200 630 42 1
8 0 40320 141120 141120 58800 11760 1176 56 1
9 0 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">factorial: function [n]-> product 1..n
 
lah: function [n,k][
if k=1 -> return factorial n
if k=n -> return 1
if k>n -> return 0
if or? k<1 n<1 -> return 0
return (((factorial n)*factorial n-1) / ((factorial k) * factorial k-1)) / factorial n-k
]
 
print @["n/k"] ++ map to [:string] 1..12 's -> pad s 10
print repeat "-" 136
loop 1..12 'x [
print @[pad to :string x 3] ++ map to [:string] map 1..12 'y -> lah x y 's -> pad s 10
]</syntaxhighlight>
 
{{out}}
 
<pre>n/k 1 2 3 4 5 6 7 8 9 10 11 12
----------------------------------------------------------------------------------------------------------------------------------------
1 1 0 0 0 0 0 0 0 0 0 0 0
2 2 1 0 0 0 0 0 0 0 0 0 0
3 6 6 1 0 0 0 0 0 0 0 0 0
4 24 36 12 1 0 0 0 0 0 0 0 0
5 120 240 120 20 1 0 0 0 0 0 0 0
6 720 1800 1200 300 30 1 0 0 0 0 0 0
7 5040 15120 12600 4200 630 42 1 0 0 0 0 0
8 40320 141120 141120 58800 11760 1176 56 1 0 0 0 0
9 362880 1451520 1693440 846720 211680 28224 2016 72 1 0 0 0
10 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1 0 0
11 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1 0
12 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LAH_NUMBERS.AWK
# converted from C
Line 70 ⟶ 352:
return (factorial(n) * factorial(n-1)) / (factorial(k) * factorial(k-1)) / factorial(n-k)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 89 ⟶ 371:
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
</pre>
 
=={{header|BQN}}==
<code>F</code> is a function that computes the factorial of a number. It returns 0 for negative numbers.
 
<code>Lah</code> computes the lah function, called as <code>n Lah k</code>.
 
The last line builds a table where the rows represent <code>n</code> and columns represent <code>k</code>.
<syntaxhighlight lang="bqn">F ← (≥⟜0)◶⟨0,×´1+↕⟩
Lah ← {
𝕨 𝕊 0: 0;
0 𝕊 𝕩: 0;
𝕨 𝕊 1: F 𝕨;
n 𝕊 k:
(n=k)⊑⟨((n ×○F n-1) ÷ k ×○F k-1) (0=⊢)◶÷‿0 F n-k, 1⟩
}
 
•Show Lah⌜˜↕12</syntaxhighlight><syntaxhighlight lang="text">┌─
╵ 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0
0 2 1 0 0 0 0 0 0 0 0 0
0 6 6 1 0 0 0 0 0 0 0 0
0 24 36 12 1 0 0 0 0 0 0 0
0 120 240 120 20 1 0 0 0 0 0 0
0 720 1800 1200 300 30 1 0 0 0 0 0
0 5040 15120 12600 4200 630 42 1 0 0 0 0
0 40320 141120 141120 58800 11760 1176 56 1 0 0 0
0 362880 1451520 1693440 846720 211680 28224 2016 72 1 0 0
0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1 0
0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
┘</syntaxhighlight>
[https://mlochbaum.github.io/BQN/try.html#code=RiDihpAgKOKJpeKfnDAp4pe24p+oMCzDl8K0MSvihpXin6kKTGFoIOKGkCB7CiAg8J2VqCDwnZWKIDA6IDA7CiAgMCDwnZWKIPCdlak6IDA7CiAg8J2VqCDwnZWKIDE6IEYg8J2VqDsKICBuIPCdlYogazoKICAobj1rKeKKkeKfqCgobiDDl+KXi0Ygbi0xKSDDtyBrIMOX4peLRiBrLTEpICgwPeKKoinil7bDt+KAvzAgRiBuLWssIDHin6kKfQoK4oCiU2hvdyBMYWjijJzLnOKGlTEy Try It!]
 
=={{header|C}}==
{{trans|D}}
<langsyntaxhighlight lang="c">#include <stdint.h>
#include <stdio.h>
 
Line 128 ⟶ 442:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 148 ⟶ 462:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Numerics;
Line 191 ⟶ 505:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>0 1
Line 212 ⟶ 526:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">// Reference: https://en.wikipedia.org/wiki/Lah_number#Identities_and_relations
 
#include <algorithm>
Line 265 ⟶ 579:
std::cout << max << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 289 ⟶ 603:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.algorithm : map;
import std.bigint;
import std.range;
Line 343 ⟶ 657:
auto lambda = (int a) => lah(BigInt(100), BigInt(a));
writeln(iota(0, 100).map!lambda.max);
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 363 ⟶ 677:
Maximum value from the L(100, *) row:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|Dyalect}}==
 
<syntaxhighlight lang="dyalect">func fact(n) =>
n is 0 ? 1 : (n^-1..<0).Fold(1, (acc, val) => acc * val)
 
func lah(n, k) {
return fact(n) when k is 1
return 1 when k == n
return 0 when k > n || k < 1 || n < 1
(fact(n) * fact(n - 1)) / (fact(k) * fact(k - 1)) / fact(n - k)
}
 
print("Unsigned Lah numbers: L(n, k):");
print("n/k ", terminator: "");
(0..12).ForEach(i => i >> "{0,10} ".Format >> print(terminator: ""))
print("")
(0..12).ForEach(row => {
row >> "{0,-3}".Format >> print(terminator: "")
(0..row).ForEach(i => lah(row, i) >> "{0,11}".Format >> print(terminator: ""))
print("")
})</syntaxhighlight>
 
{{out}}
 
<pre>Unsigned Lah numbers: L(n, k):
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 2 1
3 0 6 6 1
4 0 24 36 12 1
5 0 120 240 120 20 1
6 0 720 1800 1200 300 30 1
7 0 5040 15120 12600 4200 630 42 1
8 0 40320 141120 141120 58800 11760 1176 56 1
9 0 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func fac n .
r = 1
for i = 2 to n
r *= i
.
return r
.
print 0
print "0 1"
for n = 2 to 12
write 0 & " " & fac n & " "
for k = 2 to n - 1
write fac n * fac (n - 1) / (fac k * fac (k - 1)) / fac (n - k) & " "
.
print 1 & " "
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Lah numbers. Nigel Galloway: January 3rd., 2023
let fact(n:int)=let rec fact=function n when n=0I->1I |n->n*fact(n-1I) in fact(bigint n)
let rec lah=function (_,0)|(0,_)->0I |(n,1)->fact n |(n,g) when n=g->1I |(n,g)->((fact n)*(fact(n-1)))/((fact g)*(fact(g-1)))/(fact(n-g))
for n in {0..12} do (for g in {0..n} do printf $"%A{lah(n,g)} "); printfn ""
printfn $"\n\n%A{seq{for n in 1..99->lah(100,n)}|>Seq.max}"
</syntaxhighlight>
{{out}}
<pre>
0
0 1
0 2 1
0 6 6 1
0 24 36 12 1
0 120 240 120 20 1
0 720 1800 1200 300 30 1
0 5040 15120 12600 4200 630 42 1
0 40320 141120 141120 58800 11760 1176 56 1
0 362880 1451520 1693440 846720 211680 28224 2016 72 1
0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
 
 
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 development version 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: combinators combinators.short-circuit formatting infix io
kernel locals math math.factorials math.ranges prettyprint
sequences ;
Line 397 ⟶ 799:
 
"Maximum value from the 100 _ lah row:" print
100 [0,b] [ 100 swap lah ] map supremum .</langsyntaxhighlight>
{{out}}
<pre>
Line 421 ⟶ 823:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">function factorial( n as uinteger ) as ulongint
if n = 0 then return 1
return n*factorial(n-1)
Line 455 ⟶ 857:
next k
print outstr
next n</langsyntaxhighlight>
{{out}}
<pre>
Line 479 ⟶ 881:
=={{header|Fōrmulæ}}==
 
In [{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/Lah_numbers this] page you can see the solution of this task.}}
 
'''Solution'''
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The following function reduces to the Lah number n, k
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
If you want a unsigned Lah number, use 1 as factor. If you want a signed Lah number, use -1 as factor.
 
[[File:Fōrmulæ - Lah numbers 01.png]]
 
'''Case 1. Using the routine, generate and show a table showing the signed Lah numbers, L(n, k), up to L(12, 12)'''
 
[[File:Fōrmulæ - Lah numbers 02.png]]
 
[[File:Fōrmulæ - Lah numbers 03.png]]
 
'''Case 2. Using the routine, generate and show a table showing the unsigned Lah numbers, L(n, k), up to L(12, 12)'''
 
[[File:Fōrmulæ - Lah numbers 04.png]]
 
[[File:Fōrmulæ - Lah numbers 05.png]]
 
'''Case 3. If your language supports large integers, find and show the maximum value of unsigned L(n, k) where n ≤ 100'''
 
[[File:Fōrmulæ - Lah numbers 06.png]]
 
[[File:Fōrmulæ - Lah numbers 07.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 547 ⟶ 971:
fmt.Println(max)
fmt.Printf("which has %d digits.\n", len(max.String()))
}</langsyntaxhighlight>
 
{{out}}
Line 574 ⟶ 998:
=={{header|Haskell}}==
{{trans|Python}}
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
import Control.Monad (when)
Line 603 ⟶ 1,027:
printf "\nMaximum value from the L(100, *) row:\n%d\n"
(maximum $ lah 100 <$> ([0..100] :: [Integer]))
where zeroToTwelve = [0..12]</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 625 ⟶ 1,049:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
NB. use: k lah n
lah=: ! :(!&<: * %&!~)&x: NB. `%~' is shorter than `*inv'
Line 643 ⟶ 1,067:
extend_precision =: x: Monad
wordy_lah =: ((combinations but_1st decrement) times (into but_1st factorial))but_1st extend_precision Dyad
</syntaxhighlight>
</lang>
<pre>
lah&>~table~>:i.12
Line 678 ⟶ 1,102:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.HashMap;
Line 736 ⟶ 1,160:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 759 ⟶ 1,183:
</pre>
 
=={{header|jq}}==
 
The table can be produced by either jq or gojq (the Go implementation of jq),
but gojq is required to produce the precise maximum value.
 
<syntaxhighlight lang="jq">## Generic functions
 
def factorial: reduce range(2;.+1) as $i (1; . * $i);
 
# nCk assuming n >= k
def binomial($n; $k):
if $k > $n / 2 then binomial($n; $n-$k)
else (reduce range($k+1; $n+1) as $i (1; . * $i)) as $numerator
| reduce range(1;1+$n-$k) as $i ($numerator; . / $i)
end;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def max(s): reduce s as $x (-infinite; if $x > . then $x else . end);
 
def lah($n; $k; $signed):
if $n == $k then 1
elif $n == 0 or $k == 0 or $k > $n then 0
elif $k == 1 then $n|factorial
else
(binomial($n; $k) * binomial($n - 1; $k - 1) * (($n - $k)|factorial)) as $unsignedvalue
| if $signed and ($n % 1 == 1)
then -$unsignedvalue
else $unsignedvalue
end
end;
 
def lah($n; $k): lah($n;$k;false);
</syntaxhighlight>
'''The task'''
<syntaxhighlight lang="jq">def printlahtable($kmax):
def pad: lpad(12);
reduce range(0;$kmax+1) as $k ("n/k"|lpad(4); . + ($k|pad)),
(range(0; $kmax+1) as $n
| reduce range(0;$n+1) as $k ($n|lpad(4);
. + (lah($n; $k) | pad)) ) ;
 
def task:
"Unsigned Lah numbers up to n==12",
printlahtable(12), "",
"The maxiumum of lah(100, _) is: \(max( lah(100; range(0;101)) ))"
;
 
task
</syntaxhighlight>
{{out}}
<pre>
Unsigned Lah numbers up to n==12
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 2 1
3 0 6 6 1
4 0 24 36 12 1
5 0 120 240 120 20 1
6 0 720 1800 1200 300 30 1
7 0 5040 15120 12600 4200 630 42 1
8 0 40320 141120 141120 58800 11760 1176 56 1
9 0 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
 
The maxiumum of lah(100, _) is: 44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000
</pre>
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
function lah(n::Integer, k::Integer, signed=false)
Line 792 ⟶ 1,287:
 
println("\nThe maxiumum of lah(100, _) is: ", maximum(k -> lah(BigInt(100), BigInt(k)), 1:100))
</langsyntaxhighlight>{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 10 11 12
Line 814 ⟶ 1,309:
=={{header|Kotlin}}==
{{trans|Perl}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
 
fun factorial(n: BigInteger): BigInteger {
Line 853 ⟶ 1,348:
println("\nMaximum value from the L(100, *) row:")
println((0..100).map { lah(BigInteger.valueOf(100.toLong()), BigInteger.valueOf(it.toLong())) }.max())
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 873 ⟶ 1,368:
Maximum value from the L(100, *) row:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[Lah]
Lah[n_?Positive, 0] := 0
Lah[0, k_?Positive] := 0
Lah[n_, n_] := 1
Lah[n_, 1] := n!
Lah[n_, k_] := (-1)^n (n! ((n - 1)!))/((k! ((k - 1)!)) ((n - k)!))
Table[Lah[i, j], {i, 0, 12}, {j, 0, 12}] // Grid
Max[Lah[100, Range[0, 100]]]</syntaxhighlight>
{{out}}
<pre>1 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0
0 2 1 0 0 0 0 0 0 0 0 0 0
0 6 -6 1 0 0 0 0 0 0 0 0 0
0 24 36 12 1 0 0 0 0 0 0 0 0
0 120 -240 -120 -20 1 0 0 0 0 0 0 0
0 720 1800 1200 300 30 1 0 0 0 0 0 0
0 5040 -15120 -12600 -4200 -630 -42 1 0 0 0 0 0
0 40320 141120 141120 58800 11760 1176 56 1 0 0 0 0
0 362880 -1451520 -1693440 -846720 -211680 -28224 -2016 -72 1 0 0 0
0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1 0 0
0 39916800 -199584000 -299376000 -199584000 -69854400 -13970880 -1663200 -118800 -4950 -110 1 0
0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
 
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a row of Lah triangle */
lah(n):=append([0],makelist((binomial(n,k)*(n-1)!)/(k-1)!,k,1,n))$
 
/* Test cases */
block(makelist(lah(i),i,0,12),table_form(%%));
</syntaxhighlight>
[[File:LahTriangleMaxima.png|thumb|center]]
<syntaxhighlight lang="maxima">
lmax(lah(100));
</syntaxhighlight>
{{out}}
<pre>
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000
</pre>
 
=={{header|Nim}}==
{{trans|Julia}}
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import math, strutils
import bignum
 
Line 905 ⟶ 1,443:
let val = lah(n, k)
if val > maxval: maxval = val
echo "\nThe maximum value of lah(100, k) is ", maxval</langsyntaxhighlight>
 
{{out}}
Line 928 ⟶ 1,466:
{{libheader|ntheory}}
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 956 ⟶ 1,494:
 
say "\nMaximum value from the L(100, *) row:";
say max map { Lah(100,$_) } 0..100;</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 980 ⟶ 1,518:
{{libheader|Phix/mpfr}}
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include mpfr.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
constant lim = 100,
lim1 = lim+1,
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span><span style="color: #0000FF;">,</span>
last = 12
<span style="color: #000000;">lim1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
sequence l = repeat(0,lim1)
<span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">12</span>
for n=1 to lim1 do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lim1</span><span style="color: #0000FF;">)</span>
l[n] = mpz_inits(lim1)
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lim1</span> <span style="color: #008080;">do</span>
mpz_set_si(l[n][n],1)
<span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lim1</span><span style="color: #0000FF;">)</span>
if n!=2 then
<span style="color: #7060A8;">mpz_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">][</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
mpz_fac_ui(l[n][2],n-1)
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
end if
<span style="color: #7060A8;">mpz_fac_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
mpz {t, m100} = mpz_inits(2)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for n=1 to lim do
<span style="color: #004080;">mpz</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m100</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
for k=1 to n do
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lim</span> <span style="color: #008080;">do</span>
mpz_mul(t,l[n+1][2],l[n][2])
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
mpz_fdiv_q(t, t, l[k+1][2])
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
mpz_fdiv_q(t, t, l[k][2])
<span style="color: #7060A8;">mpz_fdiv_q</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
mpz_fdiv_q(l[n+1][k+1], t, l[n-k+1][2])
<span style="color: #7060A8;">mpz_fdiv_q</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
end for
<span style="color: #7060A8;">mpz_fdiv_q</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"Unsigned Lah numbers: l(n, k):\n n k:")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=0 to last do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Unsigned Lah numbers: l(n, k):\n n k:"</span><span style="color: #0000FF;">)</span>
printf(1,"%6d ", i)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">last</span> <span style="color: #008080;">do</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%6d "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
printf(1,"\n--- %s\n",repeat('-',last*11+6))
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for n=0 to last do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n--- %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'-'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">last</span><span style="color: #0000FF;">*</span><span style="color: #000000;">11</span><span style="color: #0000FF;">+</span><span style="color: #000000;">6</span><span style="color: #0000FF;">))</span>
printf(1,"%2d ", n)
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">last</span> <span style="color: #008080;">do</span>
for k=1 to n+1 do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
mpfr_printf(1,"%10Zd ", l[n+1][k])
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%10s "</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">])})</span>
printf(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
for k=1 to lim1 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
mpz l100k = l[lim1][k]
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lim1</span> <span style="color: #008080;">do</span>
if mpz_cmp(l100k,m100) > 0 then
<span style="color: #004080;">mpz</span> <span style="color: #000000;">l100k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">lim1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
mpz_set(m100,l100k)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l100k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m100</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l100k</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"\nThe maximum l(100,k): %s\n",shorten(mpz_get_str(m100)))</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nThe maximum l(100,k): %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m100</span><span style="color: #0000FF;">)))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,044 ⟶ 1,585:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de fact (N)
(if (=0 N)
1
Line 1,068 ⟶ 1,609:
(prinl "Maximum value from the L(100, *) row:")
(maxi '((N) (lah 100 N)) (range 0 100))
(prinl @@)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,091 ⟶ 1,632:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">% Reference: https://en.wikipedia.org/wiki/Lah_number#Identities_and_relations
 
:- dynamic unsigned_lah_number_cache/3.
Line 1,134 ⟶ 1,675:
writeln('Maximum value of L(n,k) where n = 100:'),
max_unsigned_lah_number(100, M),
writeln(M).</langsyntaxhighlight>
 
{{out}}
Line 1,156 ⟶ 1,697:
 
=={{header|Python}}==
<syntaxhighlight lang="python">from math import (comb,
<lang python>def factorial(n):
if n == 0: factorial)
return 1
res = 1
while n > 0:
res *= n
n -= 1
return res
 
 
def lah(n,k):
def lah(n, k):
if k == 1:
return factorial(n)
Line 1,174 ⟶ 1,710:
if k < 1 or n < 1:
return 0
return (factorialcomb(n, k) * factorial(n - 1)) / (factorial(k) */ factorial(k - 1)) / factorial(n - k)
 
 
def main():
print ("Unsigned Lah numbers: L(n, k):")
print ("n/k ", end='\t')
for i in xrangerange(13):
print ("%11d" % i, end='\t')
print()
for row in xrangerange(13):
print ("%-4d" % row, end='\t')
for i in xrangerange(row + 1):
l = lah(row, i)
print ("%11d" % l, end='\t')
print()
print ("\nMaximum value from the L(100, *) row:")
maxValmax_val = max([lah(100, a) for a in xrangerange(100)])
print maxVal(max_val)
 
 
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,212 ⟶ 1,751:
Maximum value from the L(100, *) row:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ table ] is ! ( n --> n )
1
101 times
[ dup ' ! put
i^ 1+ * ]
drop
 
[ 2dup = iff [ 2drop 1 ] done
2dup * 0 = iff * done
dup 1 = iff [ drop ! ] done
2dup - ! unrot
dup !
swap 1 - ! *
swap dup !
swap 1 - ! *
swap /
swap / ] is lah ( n --> n )
 
[ dip number$
over size -
space swap of
swap join echo$ ] is justify ( n n --> )
 
[ table ] is colwidth ( n --> n )
13 times
[ 12 i^ lah number$
size 2 + ' colwidth put ]
 
say " k| "
13 times
[ i^ dup colwidth justify ] cr
say " n |"
char - 115 of echo$ cr
13 times
[ i^ dup
dup 2 justify say " | "
1+ times
[ dup i^ lah
i^ colwidth justify ]
drop cr ]
cr
0
101 times
[ 100 i^ lah
2dup < iff nip else drop ]
echo</syntaxhighlight>
 
{{out}}
 
<pre> k| 0 1 2 3 4 5 6 7 8 9 10 11 12
n |-------------------------------------------------------------------------------------------------------------------
0 | 1
1 | 0 1
2 | 0 2 1
3 | 0 6 6 1
4 | 0 24 36 12 1
5 | 0 120 240 120 20 1
6 | 0 720 1800 1200 300 30 1
7 | 0 5040 15120 12600 4200 630 42 1
8 | 0 40320 141120 141120 58800 11760 1176 56 1
9 | 0 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 | 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 | 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 | 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
 
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
 
=={{header|R}}==
<syntaxhighlight lang="r">Lah_numbers <- function(n, k, type = "unsigned") {
if (n == k)
return(1)
if (n == 0 | k == 0)
return(0)
if (k == 1)
return(factorial(n))
if (k > n)
return(NA)
if (type == "unsigned")
return((factorial(n) * factorial(n - 1)) / (factorial(k) * factorial(k - 1)) / factorial(n - k))
if (type == "signed")
return(-1 ** n * (factorial(n) * factorial(n - 1)) / (factorial(k) * factorial(k - 1)) / factorial(n - k))
}
 
#demo
Table <- matrix(0 , 13, 13, dimnames = list(0:12, 0:12))
 
for (n in 0:12) {
for (k in 0:12) {
Table[n + 1, k + 1] <- Lah_numbers(n, k, type = "unsigned")
}
}
 
Table
</syntaxhighlight>
{{out}}
<pre>
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 0
1 0 1 NA NA NA NA NA NA NA NA NA NA NA
2 0 2 1 NA NA NA NA NA NA NA NA NA NA
3 0 6 6 1 NA NA NA NA NA NA NA NA NA
4 0 24 36 12 1 NA NA NA NA NA NA NA NA
5 0 120 240 120 20 1 NA NA NA NA NA NA NA
6 0 720 1800 1200 300 30 1 NA NA NA NA NA NA
7 0 5040 15120 12600 4200 630 42 1 NA NA NA NA NA
8 0 40320 141120 141120 58800 11760 1176 56 1 NA NA NA NA
9 0 362880 1451520 1693440 846720 211680 28224 2016 72 1 NA NA NA
10 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1 NA NA
11 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1 NA
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1</pre>
 
=={{header|Raku}}==
Line 1,217 ⟶ 1,877:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>constant @factorial = 1, |[\*] 1..*;
 
sub Lah (Int \n, Int \k) {
Line 1,240 ⟶ 1,900:
 
say "\nMaximum value from the L(100, *) row:";
say (^100).map( { Lah 100, $_ } ).max;</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,265 ⟶ 1,925:
 
Also, code was added to use memoization of the factorial calculations.
<langsyntaxhighlight lang="rexx">/*REXX pgm computes & display (unsigned) Stirling numbers of the 3rd kind (Lah numbers).*/
parse arg lim . /*obtain optional argument from the CL.*/
if lim=='' | lim=="," then lim= 12 /*Not specified? Then use the default.*/
Line 1,305 ⟶ 1,965:
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: parse arg z; if !.z\==. then return !.z; !=1; do f=2 to z; !=!*f; end; !.z=!; return !
maxer: max#.k= max(max#.k, @.n.k); max#.b= max(max#.b, @.n.k); return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,328 ⟶ 1,988:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000
</pre>
 
=={{header|RPL}}==
'''Using local variables'''
« → n k
« '''CASE'''
n k == '''THEN''' 1 '''END'''
n k * NOT '''THEN''' 0 '''END'''
k 1 == '''THEN''' n FACT '''END'''
n 1 - k - 1 COMB n FACT * k FACT /
'''END'''
» » '<span style="color:blue">ULAH</span>' STO
'''Using the stack'''
« '''CASE'''
DUP2 == '''THEN''' DROP2 1 '''END'''
DUP2 * NOT '''THEN''' * '''END'''
DUP 1 == '''THEN''' DROP FACT '''END'''
DUP2 1 - SWAP 1 - SWAP COMB ROT FACT * SWAP FACT /
'''END'''
» '<span style="color:blue">ULAH</span>' STO
 
« { 12 12 } 0 CON
1 12 '''FOR''' n
1 n '''FOR''' k
n k 2 →LIST n k <span style="color:blue">ULAH</span> PUT
'''NEXT NEXT'''
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: [[ 1 0 0 0 0 0 0 0 0 0 0 0 ]
[ 2 1 0 0 0 0 0 0 0 0 0 0 ]
[ 6 6 1 0 0 0 0 0 0 0 0 0 ]
[ 24 36 12 1 0 0 0 0 0 0 0 0 ]
[ 120 240 120 20 1 0 0 0 0 0 0 0 ]
[ 720 1800 1200 300 30 1 0 0 0 0 0 0 ]
[ 5040 15120 12600 4200 630 42 1 0 0 0 0 0 ]
[ 40320 141120 141120 58800 11760 1176 56 1 0 0 0 0 ]
[ 362880 1451520 1693440 846720 211680 28224 2016 72 1 0 0 0 ]
[ 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1 0 0 ]
[ 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1 0 ]
[ 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1 ]]
</pre>
 
=={{header|Ruby}}==
Works with Ruby 3.0 (end-less method; end-less and begin-less range).
<syntaxhighlight lang="ruby">def fact(n) = n.zero? ? 1 : 1.upto(n).inject(&:*)
 
def lah(n, k)
case k
when 1 then fact(n)
when n then 1
when (..1),(n..) then 0
else n<1 ? 0 : (fact(n)*fact(n-1)) / (fact(k)*fact(k-1)) / fact(n-k)
end
end
r = (0..12)
puts "Unsigned Lah numbers: L(n, k):"
puts "n/k #{r.map{|n| "%11d" % n}.join}"
 
r.each do |row|
print "%-4s" % row
puts "#{(0..row).map{|col| "%11d" % lah(row,col)}.join}"
end
 
puts "\nMaximum value from the L(100, *) row:";
puts (1..100).map{|a| lah(100,a)}.max
</syntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 2 1
3 0 6 6 1
4 0 24 36 12 1
5 0 120 240 120 20 1
6 0 720 1800 1200 300 30 1
7 0 5040 15120 12600 4200 630 42 1
8 0 40320 141120 141120 58800 11760 1176 56 1
9 0 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
 
Maximum value from the L(100, *) row:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000
</pre>
 
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<syntaxhighlight lang="scheme">; Compute the Unsigned Lah number L(n, k).
(define lah
(lambda (n k)
(/ (/ (* (fact n) (fact (1- n))) (* (fact k) (fact (1- k)))) (fact (- n k)))))
 
; Procedure to compute factorial.
(define fact
(lambda (n)
(if (<= n 0)
1
(* n (fact (1- n))))))
 
; Generate a table of the Unsigned Lah numbers L(n, k) up to L(12, 12).
(printf "The Unsigned Lah numbers L(n, k) up to L(12, 12):~%")
(printf "n\\k~10d" 1)
(do ((k 2 (1+ k)))
((> k 12))
(printf " ~10d" k))
(newline)
(do ((n 1 (1+ n)))
((> n 12))
(printf "~2d" n)
(do ((k 1 (1+ k)))
((> k n))
(printf " ~10d" (lah n k)))
(newline))
 
; Find the maximum value of L(n, k) where n = 100.
(printf "~%The maximum value of L(n, k) where n = 100:~%")
(let ((max 0))
(do ((k 1 (1+ k)))
((> k 100))
(let ((val (lah 100 k)))
(when (> val max) (set! max val))))
(printf "~d~%" max))</syntaxhighlight>
{{out}}
<pre>The Unsigned Lah numbers L(n, k) up to L(12, 12):
n\k 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2 1
3 6 6 1
4 24 36 12 1
5 120 240 120 20 1
6 720 1800 1200 300 30 1
7 5040 15120 12600 4200 630 42 1
8 40320 141120 141120 58800 11760 1176 56 1
9 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
 
The maximum value of L(n, k) where n = 100:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program lah_numbers;
loop for n in [0..12] do
loop for k in [0..n] do
nprint(lpad(str lah(n,k), 11));
end loop;
print;
end loop;
 
print("Maximum value for lah(100,k):");
print(max/[lah(100,k) : k in [1..100]]);
 
op fac(n);
return */{1..n};
end op;
 
proc lah(n,k);
case of
(n=k): return 1;
(n=0 or k=0): return 0;
(k=1): return fac n;
else return (fac n*fac (n-1)) div
(fac k*fac (k-1)) div
fac (n-k);
end case;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre> 1
0 1
0 2 1
0 6 6 1
0 24 36 12 1
0 120 240 120 20 1
0 720 1800 1200 300 30 1
0 5040 15120 12600 4200 630 42 1
0 40320 141120 141120 58800 11760 1176 56 1
0 362880 1451520 1693440 846720 211680 28224 2016 72 1
0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
Maximum value for lah(100,k):
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func lah(n, k) {
stirling3(n, k)
#binomial(n-1, k-1) * n!/k! # alternative formula
Line 1,351 ⟶ 2,198:
say "\nMaximum value from the L(#{n}, *) row:"
say { lah(n, _) }.map(^n).max
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,378 ⟶ 2,225:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">import BigInt
import Foundation
 
Line 1,432 ⟶ 2,279:
let maxLah = (0...100).map({ lah(n: BigInt(100), k: BigInt($0)) }).max()!
 
print("Maximum value from the L(100, *) row: \(maxLah)")</langsyntaxhighlight>
 
{{out}}
Line 1,455 ⟶ 2,302:
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">proc prod {from to} {
set r 1
if {$from <= $to} {
Line 1,533 ⟶ 2,380:
puts "([string length $maxv] digits, k=$maxk)"
}
main </langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">Unsigned Lah numbers L(n,k):
Line 1,552 ⟶ 2,399:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000
(164 digits, k=10)
</pre>
 
=={{header|Vala}}==
<syntaxhighlight lang="vala">uint64 factorial(uint8 n) {
uint64 res = 1;
if (n == 0) return res;
while (n > 0) res *= n--;
return res;
}
 
uint64 lah(uint8 n, uint8 k) {
if (k == 1) return factorial(n);
if (k == n) return 1;
if (k > n) return 0;
if (k < 1 || n < 1) return 0;
return (factorial(n) * factorial(n - 1)) / (factorial(k) * factorial(k - 1)) / factorial(n - k);
}
 
void main() {
uint8 row, i;
print("Unsigned Lah numbers: L(n, k):\n");
print("n/k ");
for (i = 0; i < 13; i++) {
print("%10d ", i);
}
print("\n");
for (row = 0; row < 13; row++) {
print("%-3d", row);
for (i = 0; i < row + 1; i++) {
uint64 l = lah(row, i);
print("%11lld", l);
}
print("\n");
}
}</syntaxhighlight>
{{out}}
<pre style="font-size:83%">Unsigned Lah numbers: L(n, k):
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 2 1
3 0 6 6 1
4 0 24 36 12 1
5 0 120 240 120 20 1
6 0 720 1800 1200 300 30 1
7 0 5040 15120 12600 4200 630 42 1
8 0 40320 141120 141120 58800 11760 1176 56 1
9 0 362880 1451520 1693440 846720 211680 28224 2016 72 1
10 0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1
11 0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
</pre>
 
=={{header|VBScript}}==
{{trans|BBCVisual BASICBasic .NET}}
<langsyntaxhighlight lang="vb">' Lah numbers - VBScript - 04/02/2021
' cscript /nologo "Lah numbers - rc.vbs"
 
Function F(i,n)
Line 1,609 ⟶ 2,507:
End Sub 'Main
 
Main() </langsyntaxhighlight>
{{out}}
<pre>
Line 1,627 ⟶ 2,525:
12 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
</pre>
 
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Numerics
 
Module Module1
Line 1,687 ⟶ 2,584:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,707 ⟶ 2,604:
Maximum value from the L(100, *) row:
44519005448993144810881324947684737529186447692709328597242209638906324913313742508392928375354932241404408343800007105650554669129521241784320000000000000000000000</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import math.big
 
fn main() {
limit := 100
last := 12
unsigned := true
mut l := [][]big.Integer{len: limit+1}
for n := 0; n <= limit; n++ {
l[n] = []big.Integer{len: limit+1}
for k := 0; k <= limit; k++ {
l[n][k] = big.zero_int
}
l[n][n]= big.integer_from_int(1)
if n != 1 {
l[n][1]= big.integer_from_int(n).factorial()
}
}
mut t := big.zero_int
for n := 1; n <= limit; n++ {
for k := 1; k <= n; k++ {
t = l[n][1] * l[n-1][1]
t /= l[k][1]
t /= l[k-1][1]
t /= l[n-k][1]
l[n][k] = t
if !unsigned && (n%2 == 1) {
l[n][k] = l[n][k].neg()
}
}
}
println("Unsigned Lah numbers: l(n, k):")
print("n/k")
for i := 0; i <= last; i++ {
print("${i:10} ")
}
print("\n--")
for i := 0; i <= last; i++ {
print("-----------")
}
println('')
for n := 0; n <= last; n++ {
print("${n:2} ")
for k := 0; k <= n; k++ {
print("${l[n][k]:10} ")
}
println('')
}
println("\nMaximum value from the l(100, *) row:")
mut max := l[limit][0]
for k := 1; k <= limit; k++ {
if l[limit][k] > max {
max = l[limit][k]
}
}
println(max)
println("which has ${max.str().len} digits.")
}
</syntaxhighlight>
 
{{out}}
<pre>Same as Go output</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var fact = Fn.new { |n|
Line 1,729 ⟶ 2,690:
System.print("Unsigned Lah numbers: l(n, k):")
System.write("n/k")
for (i in 0..12) SystemFmt.write("%(Fmt.d(10$10d ", i)) ")
System.print("\n" + "-" * 145)
for (n in 0..12) {
SystemFmt.write("%(Fmt.d(2$2d ", n)) ")
for (k in 0..n) SystemFmt.write("%(Fmt.d(10$10d ", lah.call(n, k))) ")
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,757 ⟶ 2,718:
</pre>
 
=={{header|ValaXPL0}}==
{{trans|C}}
<lang Vala>uint64 factorial(uint8 n) {
<syntaxhighlight lang "XPL0">func real Factorial(N);
uint64 res = 1;
real N, Res;
if (n == 0) return res;
[Res:= 1.;
while (n > 0) res *= n--;
if N = 0. then return resRes;
while N > 0. do [Res:= Res*N; N:= N-1.];
}
return Res;
];
 
func real Lah(N, K);
uint64 lah(uint8 n, uint8 k) {
real N, K;
if (k == 1) return factorial(n);
[if (kK == n)1. then return 1Factorial(N);
if K if= (kN > n) then return 01.;
if (kK <> 1N || n < 1)then return 0.;
if K < 1. or N < 1. then return 0.;
return (factorial(n) * factorial(n - 1)) / (factorial(k) * factorial(k - 1)) / factorial(n - k);
return (Factorial(N) * Factorial(N-1.)) / (Factorial(K) * Factorial(K-1.)) /
}
Factorial(N-K);
];
 
int Row, I;
void main() {
[Text(0, "Unsigned Lah numbers: L(N,K):"); CrLf(0);
uint8 row, i;
Text(0, "N/K");
Format(11, 0);
print("Unsigned Lah numbers: L(n, k):\n");
for I:= 0 to 12 do RlOut(0, float(I));
print("n/k ");
CrLf(0);
for (i = 0; i < 13; i++) {
for Row:= 0 to 12 do
print("%10d ", i);
[Format(3, 0); RlOut(0, float(Row));
}
for I:= 0 to Row do
print("\n");
[Format(11, 0); RlOut(0, Lah(float(Row), float(I)))];
for (row = 0; row < 13; row++) {
printCrLf("%-3d", row0);
]
for (i = 0; i < row + 1; i++) {
]</syntaxhighlight>
uint64 l = lah(row, i);
print("%11lld", l);
}
print("\n");
}
}</lang>
{{out}}
<pre style="font-size:8366%">Unsigned Lah numbers: L(n, k):
Unsigned Lah numbers: L(N,K):
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
N/K 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 1 0 2 1
3 2 0 6 62 1
4 3 0 24 6 36 126 1
5 4 0 120 24 240 36 120 2012 1
6 5 0 720120 1800 240 1200 120 300 3020 1
7 6 0 5040 720 15120 1800 12600 1200 4200 300 630 4230 1
8 7 0 40320 5040 141120 15120 141120 12600 58800 4200 11760 630 1176 5642 1
9 8 0 362880 40320 1451520 141120 1693440 141120 846720 58800 211680 11760 28224 1176 2016 7256 1
10 9 0 3628800 362880 16329600 1451520 21772800 1693440 12700800 3810240846720 635040211680 6048028224 32402016 9072 1
11 10 0 39916800 3628800 16329600 19958400021772800 299376000 12700800 199584000 698544003810240 13970880 635040 1663200 60480 118800 3240 4950 11090 1
12 11 0 479001600 263450880039916800 199584000 299376000 4390848000 3293136000199584000 1317254400 30735936069854400 4390848013970880 39204001663200 217800118800 72604950 132110 1
12 0 479001600 2634508800 4390848000 3293136000 1317254400 307359360 43908480 3920400 217800 7260 132 1
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn lah(n,k,fact=fcn(n){ [1..n].reduce('*,1) }){
if(n==k) return(1);
if(k==1) return(fact(n));
if(n<1 or k<1) return(0);
(fact(n)*fact(n - 1)) /(fact(k)*fact(k - 1)) /fact(n - k)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">// calculate entire table (quick), find max, find num digits in max
N,mx := 12, [1..N].apply(fcn(n){ [1..n].apply(lah.fp(n)) }).flatten() : (0).max(_);
fmt:="%%%dd".fmt("%d".fmt(mx.numDigits + 1)).fmt; // "%9d".fmt
Line 1,823 ⟶ 2,784:
foreach row in ([0..N]){
println("%3d".fmt(row), [0..row].pump(String, lah.fp(row), fmt));
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 1,843 ⟶ 2,804:
</pre>
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
N=100;
L100:=[1..N].apply(lah.fpM("101",BI(N),fcn(n){ BI(n).factorial() }))
.reduce(fcn(m,n){ m.max(n) });
println("Maximum value from the L(%d, *) row (%d digits):".fmt(N,L100.numDigits));
println(L100);</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
1,150

edits