Stirling numbers of the second kind: Difference between revisions

added RPL
(++ Tcl)
(added RPL)
 
(42 intermediate revisions by 19 users not shown)
Line 35:
 
<br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">[(Int, Int) = BigInt] computed
 
F sterling2(n, k)
V key = (n, k)
 
I key C :computed
R :computed[key]
I n == k == 0
R BigInt(1)
I (n > 0 & k == 0) | (n == 0 & k > 0)
R BigInt(0)
I n == k
R BigInt(1)
I k > n
R BigInt(0)
V result = k * sterling2(n - 1, k) + sterling2(n - 1, k - 1)
:computed[key] = result
R result
 
print(‘Stirling numbers of the second kind:’)
V MAX = 12
print(‘n/k’.ljust(10), end' ‘’)
L(n) 0 .. MAX
print(String(n).rjust(10), end' ‘’)
print()
L(n) 0 .. MAX
print(String(n).ljust(10), end' ‘’)
L(k) 0 .. n
print(String(sterling2(n, k)).rjust(10), end' ‘’)
print()
print(‘The maximum value of S2(100, k) = ’)
BigInt previous = 0
L(k) 1 .. 100
V current = sterling2(100, k)
I current > previous
previous = current
E
print("#.\n(#. digits, k = #.)\n".format(previous, String(previous).len, k - 1))
L.break</syntaxhighlight>
 
{{out}}
<pre>
Stirling numbers of the second kind:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
The maximum value of S2(100, k) =
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses the LONG LONG INT mode of Algol 68g which allows large precision integers. As the default precision of LONG LONG INT is too small, the precision is specified via a pragmatic comment.
<langsyntaxhighlight lang="algol68">BEGIN
BEGIN # show some Stirling numbers of the second kind #
 
# specify the precision of LONG LONG INT, somewhat under 160 digits are #
Line 55 ⟶ 120:
FOR n FROM 0 TO max n - 1 DO
FOR k FROM 1 TO n DO
s2[ n + 1, k ] := k * s2[ n, k ] + s2[ n, k - 1 ];
OD
OD;
Line 67 ⟶ 132:
print( ( "Stirling numbers of the second kind:", newline ) );
print( ( " k" ) );
FOR k FROM 0 TO max stirling DO print( ( whole( k, -108 ) ) ) OD;
print( ( newline, " n", newline ) );
FOR n FROM 0 TO max stirling DO
print( ( whole( n, -2 ) ) );
FOR k FROM 0 TO n DO
print( ( whole( s2[ n, k ], -108 ) ) )
OD;
print( ( newline ) )
Line 88 ⟶ 153:
print( ( whole( max 100, 0 ), newline ) )
END
END</lang>
</syntaxhighlight>
{{out}}
<pre>
Stirling numbers of the second kind:
k 0 1 2 3 4 5 6 7 8 9 10 11 12
n
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
Maximum Stirling number of the second kind with n = 100:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin % show some Stirling numbers of the second kind %
integer MAX_STIRLING;
MAX_STIRLING := 12;
begin
% construct a matrix of Stirling numbers up to max n, max n %
integer array s2 ( 0 :: MAX_STIRLING, 0 :: MAX_STIRLING );
for n := 0 until MAX_STIRLING do begin
for k := 0 until MAX_STIRLING do s2( n, k ) := 0
end for_n ;
for n := 0 until MAX_STIRLING do s2( n, n ) := 1;
for n := 0 until MAX_STIRLING - 1 do begin
for k := 1 until n do begin
s2( n + 1, k ) := k * s2( n, k ) + s2( n, k - 1 );
end for_k
end for_n ;
% print the Stirling numbers %
write( "Stirling numbers of the second kind:" );
write( " k" );
for k := 0 until MAX_STIRLING do writeon( i_w := 8, s_w := 0, k );
write( " n" );
for n := 0 until MAX_STIRLING do begin
write( i_w := 2, s_w := 0, n );
for k := 0 until n do writeon( i_w := 8, s_w := 0, s2( n, k ) );
end for_n
end
end.
</syntaxhighlight>
{{out}}
<pre>
Stirling numbers of the second kind:
k 0 1 2 3 4 5 6 7 8 9 10 11 12
n
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
</pre>
 
=={{header|BASIC}}==
<syntaxhighlight lang="basic">10 DEFINT N,K: DEFDBL S: DEFSTR F
20 DIM S2(12,12),F(12)
30 FOR N=0 TO 12: READ F(N): NEXT N
40 S2(0,0)=1
50 FOR K=1 TO 12
60 FOR N=1 TO 12
70 IF N=K THEN S2(N,K)=1 ELSE S2(N,K)=K*S2(N-1,K)+S2(N-1,K-1)
80 NEXT N,K
90 FOR N=0 TO 12
100 FOR K=0 TO 12
110 IF N>=K THEN PRINT USING F(K);S2(N,K);
120 NEXT K
130 PRINT
140 NEXT N
150 DATA ##,##,#####,######,#######,########
160 DATA ########,#######,#######,######,#####,###,##</syntaxhighlight>
{{out}}
<pre> 1
0 1
0 1 1
0 1 3 1
0 1 7 6 1
0 1 15 25 10 1
0 1 31 90 65 15 1
0 1 63 301 350 140 21 1
0 1 127 966 1701 1050 266 28 1
0 1 255 3025 7770 6951 2646 462 36 1
0 1 511 9330 34105 42525 22827 5880 750 45 1
0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
 
typedef struct stirling_cache_tag {
int max;
int* values;
} stirling_cache;
 
int stirling_number2(stirling_cache* sc, int n, int k) {
if (k == n)
return 1;
if (k == 0 || k > n || n > sc->max)
return 0;
return sc->values[n*(n-1)/2 + k - 1];
}
 
bool stirling_cache_create(stirling_cache* sc, int max) {
int* values = calloc(max * (max + 1)/2, sizeof(int));
if (values == NULL)
return false;
sc->max = max;
sc->values = values;
for (int n = 1; n <= max; ++n) {
for (int k = 1; k < n; ++k) {
int s1 = stirling_number2(sc, n - 1, k - 1);
int s2 = stirling_number2(sc, n - 1, k);
values[n*(n-1)/2 + k - 1] = s1 + s2 * k;
}
}
return true;
}
 
void stirling_cache_destroy(stirling_cache* sc) {
free(sc->values);
sc->values = NULL;
}
 
void print_stirling_numbers(stirling_cache* sc, int max) {
printf("Stirling numbers of the second kind:\nn/k");
for (int k = 0; k <= max; ++k)
printf(k == 0 ? "%2d" : "%8d", k);
printf("\n");
for (int n = 0; n <= max; ++n) {
printf("%2d ", n);
for (int k = 0; k <= n; ++k)
printf(k == 0 ? "%2d" : "%8d", stirling_number2(sc, n, k));
printf("\n");
}
}
 
int main() {
stirling_cache sc = { 0 };
const int max = 12;
if (!stirling_cache_create(&sc, max)) {
fprintf(stderr, "Out of memory\n");
return 1;
}
print_stirling_numbers(&sc, max);
stirling_cache_destroy(&sc);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Stirling numbers of the second kind:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
</pre>
 
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <cstdintalgorithm>
#include <iomanip>
#include <iostream>
Line 121 ⟶ 350:
using integer = mpz_class;
 
class stirling2 {
{
public:
integer get(int n, int k);
Line 129 ⟶ 357:
};
 
integer stirling2::get(int n, int k) {
{
if (k == n)
return 1;
Line 144 ⟶ 371:
}
 
void print_stirling_numbers(stirling2& s2, int n) {
std::cout << "Stirling numbers of the second kind:\nn/k";
{
for (int ij = 0; ij <= n; ++ij) {
std::cout << std::setw(j == 0 ? 2 : 8) << j;
{
}
std::cout << '\n';
for (int i = 0; i <= n; ++i) {
std::cout << std::setw(2) << i << ' ';
for (int j = 0; j <= i; ++j)
std::cout << std::setw(j == 0 ? 2 : 8) << s2.get(i, j);
std::cout << '\n';
}
}
 
int main() {
{
stirling2 s2;
std::cout << "Stirling numbers of the second kind:\n";
print_stirling_numbers(s2, 12);
std::cout << "Maximum value of S2(n,k) where n == 100:\n";
integer max = 0;
for (int k = 0; k <= 100; ++k)
max = std::max(max, s2.get(100, k));
{
integer s = s2.get(100, k);
if (s > max)
max = s;
}
std::cout << max << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
<pre>
Stirling numbers of the second kind:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
1
0 1
1 0 1 1
2 0 1 3 1
3 0 1 7 63 1
4 0 1 15 7 25 106 1
5 0 1 3115 9025 65 1510 1
6 0 1 63 31 301 90 350 14065 2115 1
7 0 1 127 63 966 301 1701 1050350 266140 2821 1
8 0 1 255 127 3025 7770966 69511701 26461050 462266 3628 1
9 0 1 511 255 9330 3025 34105 7770 42525 228276951 58802646 750462 4536 1
10 0 0 1 1 511 1023 9330 28501 34105 145750 24673042525 179487 22827 63987 5880 11880 1155750 5545 1
11 0 1 20471023 28501 86526 145750 611501 1379400246730 1323652 179487 627396 15902763987 2227511880 17051155 6655 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
Maximum value of S2(n,k) where n == 100:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
</pre>
 
=={{header|D}}==
{{trans|Java}}
<syntaxhighlight lang="d">import std.bigint;
import std.conv;
import std.functional;
import std.stdio;
 
alias sterling2 = memoize!sterling2Impl;
BigInt sterling2Impl(int n, int k) {
if (n == 0 && k == 0) {
return BigInt(1);
}
if ((n > 0 && k == 0) || (n == 0 && k > 0)) {
return BigInt(0);
}
if (n == k) {
return BigInt(1);
}
if (k > n) {
return BigInt(0);
}
 
return BigInt(k) * sterling2(n - 1, k) + sterling2(n - 1, k - 1);
}
 
void main() {
writeln("Stirling numbers of the second kind:");
int max = 12;
 
write("n/k");
for (int n = 0; n <= max; n++) {
writef("%10d", n);
}
writeln;
 
for (int n = 0; n <= max; n++) {
writef("%-3d", n);
for (int k = 0; k <= n; k++) {
writef("%10s", sterling2(n, k));
}
writeln;
}
 
writeln("The maximum value of S2(100, k) = ");
auto previous = BigInt(0);
for (int k = 1; k <= 100; k++) {
auto current = sterling2(100, k);
if (current > previous) {
previous = current;
} else {
writeln(previous);
auto ps = previous.to!string;
writefln("(%d digits, k = %d)", ps.length, k - 1);
break;
}
}
}</syntaxhighlight>
{{out}}
<pre>Stirling numbers of the second kind:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
The maximum value of S2(100, k) =
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
print "Unsigned Stirling numbers of the second kind:"
len a[] 13 ; arrbase a[] 0
len b[] 13 ; arrbase b[] 0
a[0] = 1
print 1
for n = 1 to 12
b[0] = 0
write 0 & " "
for k = 1 to n - 1
b[k] = k * a[k] + a[k - 1]
write b[k] & " "
.
b[n] = 1
write 1 & " "
print ""
swap a[] b[]
.
</syntaxhighlight>
 
=={{header|Factor}}==
{{works with|Factor|0.99 development version 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit formatting io kernel math
math.extras prettyprint sequences ;
RENAME: stirling math.extras => (stirling)
Line 213 ⟶ 537:
 
"Maximum value from the 100 _ stirling row:" print
100 <iota> [ 100 swap stirling ] map supremum .</langsyntaxhighlight>
{{out}}
<pre>
Line 235 ⟶ 559:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">dim as integer S2(0 to 12, 0 to 12) 'initially set with zeroes
dim as ubyte n, k
dim as string outstr
 
function padto( i as ubyte, j as integer ) as string
return wspace(i-len(str(j)))+str(j)
end function
 
for k = 0 to 12 'calculate table
S2(k,k)=1
next k
for n = 1 to 11
for k = 1 to 12
S2(n+1,k) = k*S2(n,k) + S2(n,k-1)
next k
next n
 
print "Stirling numbers of the second kind"
print
outstr = " k"
for k=0 to 12
outstr += padto(12, k)
next k
print outstr
print " n"
for n = 0 to 12
outstr = padto(2, n)+" "
for k = 0 to 12
outstr += padto(12, S2(n, k))
next k
print outstr
next n</syntaxhighlight>
<pre>Stirling numbers of the second kind
 
k 0 1 2 3 4 5 6 7 8 9 10 11 12
n
0 1 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0 0 0 0 0
2 0 1 1 0 0 0 0 0 0 0 0 0 0
3 0 1 3 1 0 0 0 0 0 0 0 0 0
4 0 1 7 6 1 0 0 0 0 0 0 0 0
5 0 1 15 25 10 1 0 0 0 0 0 0 0
6 0 1 31 90 65 15 1 0 0 0 0 0 0
7 0 1 63 301 350 140 21 1 0 0 0 0 0
8 0 1 127 966 1701 1050 266 28 1 0 0 0 0
9 0 1 255 3025 7770 6951 2646 462 36 1 0 0 0
10 0 1 511 9330 34105 42525 22827 5880 750 45 1 0 0
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1 0
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1</pre>
 
=={{header|Fōrmulæ}}==
 
In [{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/Stirling_numbers_of_the_second_kind this] page you can see the solution of this task.}}
 
'''Solution'''
 
'''Version 1. Recursive'''
 
[[File:Fōrmulæ - Stirling numbers of the second kind 01.png]]
 
'''Test case 1. Show the Stirling numbers of the second kind, S₂(n, k), up to S₂(12, 12)'''
 
[[File:Fōrmulæ - Stirling numbers of the second kind 02.png]]
 
[[File:Fōrmulæ - Stirling numbers of the second kind 03.png]]
 
'''Version 2. Non recursive'''
 
A faster, non recursive version is presented. This constructs a matrix.
 
[[File:Fōrmulæ - Stirling numbers of the second kind 04.png]]
 
'''Test case 1. Show the Stirling numbers of the second kind, S₂(n, k), up to S₂(12, 12)'''
 
[[File:Fōrmulæ - Stirling numbers of the second kind 05.png]]
 
(the result is the same as recursive version)
 
'''Test case 2. Find the maximum value of S₂(n, k) where n ≤ 100'''
 
[[File:Fōrmulæ - Stirling numbers of the second kind 06.png]]
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.
 
[[File:Fōrmulæ - Stirling numbers of the second kind 07.png]]
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 297 ⟶ 698:
fmt.Println(max)
fmt.Printf("which has %d digits.\n", len(max.String()))
}</langsyntaxhighlight>
 
{{out}}
Line 322 ⟶ 723:
which has 115 digits.
</pre>
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Text.Printf (printf)
import Data.List (groupBy)
import qualified Data.MemoCombinators as Memo
 
stirling2 :: Integral a => (a, a) -> a
stirling2 = Memo.pair Memo.integral Memo.integral f
where
f (n, k)
| n == 0 && k == 0 = 1
| (n > 0 && k == 0) || (n == 0 && k > 0) = 0
| n == k = 1
| k > n = 0
| otherwise = k * stirling2 (pred n, k) + stirling2 (pred n, pred k)
 
main :: IO ()
main = do
printf "n/k"
mapM_ (printf "%10d") ([0..12] :: [Int]) >> printf "\n"
printf "%s\n" $ replicate (13 * 10 + 3) '-'
mapM_ (\row -> printf "%2d|" (fst $ head row) >>
mapM_ (printf "%10d" . stirling2) row >> printf "\n") table
printf "\nThe maximum value of S2(100, k):\n%d\n" $
maximum ([stirling2 (100, n) | n <- [1..100]] :: [Integer])
where
table :: [[(Int, Int)]]
table = groupBy (\a b -> fst a == fst b) $ (,) <$> [0..12] <*> [0..12]</syntaxhighlight>
{{out}}
<pre>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 0
1| 0 1 0 0 0 0 0 0 0 0 0 0 0
2| 0 1 1 0 0 0 0 0 0 0 0 0 0
3| 0 1 3 1 0 0 0 0 0 0 0 0 0
4| 0 1 7 6 1 0 0 0 0 0 0 0 0
5| 0 1 15 25 10 1 0 0 0 0 0 0 0
6| 0 1 31 90 65 15 1 0 0 0 0 0 0
7| 0 1 63 301 350 140 21 1 0 0 0 0 0
8| 0 1 127 966 1701 1050 266 28 1 0 0 0 0
9| 0 1 255 3025 7770 6951 2646 462 36 1 0 0 0
10| 0 1 511 9330 34105 42525 22827 5880 750 45 1 0 0
11| 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1 0
12| 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
 
The maximum value of S2(100, k):
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674</pre>
 
=={{header|J}}==
<pre>
test=: 1 i.~ (0 = *) , =
s2=: 0:`1:`($:&<: + ] * <:@:[ $: ])@.test M.
 
s2&>table i. 13
+----+--------------------------------------------------------------------+
|s2&>|0 1 2 3 4 5 6 7 8 9 10 11 12|
+----+--------------------------------------------------------------------+
| 0 |0 0 0 0 0 0 0 0 0 0 0 0 0|
| 1 |0 1 0 0 0 0 0 0 0 0 0 0 0|
| 2 |0 1 1 0 0 0 0 0 0 0 0 0 0|
| 3 |0 1 3 1 0 0 0 0 0 0 0 0 0|
| 4 |0 1 7 6 1 0 0 0 0 0 0 0 0|
| 5 |0 1 15 25 10 1 0 0 0 0 0 0 0|
| 6 |0 1 31 90 65 15 1 0 0 0 0 0 0|
| 7 |0 1 63 301 350 140 21 1 0 0 0 0 0|
| 8 |0 1 127 966 1701 1050 266 28 1 0 0 0 0|
| 9 |0 1 255 3025 7770 6951 2646 462 36 1 0 0 0|
|10 |0 1 511 9330 34105 42525 22827 5880 750 45 1 0 0|
|11 |0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1 0|
|12 |0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1|
+----+--------------------------------------------------------------------+
 
>./ 100 s2&x:&> i. 101
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
</pre>
 
references:
 
customized power:
[https://code.jsoftware.com/wiki/Vocabulary/hat#stope]
 
rising and falling factorial:
[https://www.jsoftware.com/papers/camn.htm]
[https://www.jsoftware.com/books/pdf/calculus.pdf]
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.HashMap;
Line 385 ⟶ 869:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 405 ⟶ 889:
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
The maximum value of S2(100, k) =
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with gojq, the Go implementation of jq'''
 
The following program is more complex than it otherwise would be because
it ensures that the cache of values computed in the first part
is available in the second part.
 
<syntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Input: {computed} (the cache)
# Output: {computed, result}
def stirling2($n; $k):
"\($n),\($k)" as $key
| if .computed[$key] then .result = .computed[$key]
elif ($n == 0 and $k == 0) then .result = 1
elif (($n > 0 and $k == 0) or ($n == 0 and $k > 0)) then .result = 0
elif ($k == $n) then .result = 1
elif ($k > $n) then .result = 0
else stirling2($n-1; $k-1) as $s1
| ($s1 | stirling2($n-1; $k)) as $s2
| ($s1.result + ($k * $s2.result)) as $result
| $s2
| .computed[$key] = $result
| .result = $result
end ;
 
# Set .emit to a table of values and .computed to a cache of stirling2 values
# Output: {emit, computed}
def part1(max):
{emit: "Unsigned Stirling numbers of the second kind:"}
| .emit += "\n" + "n/k" +
([range(0; max+1) | lpad(10)] | join(""))
| reduce range(0; max+1) as $n ( .computed = {};
.emit += "\n" + ($n | lpad(3))
| reduce range(0; $n+1) as $k (.;
stirling2($n; $k)
| .emit += (.result | lpad(10) ) )) ;
 
# "The maximum value of S2($m, k) ..."
# Input: {computed}
def part2($m):
"The maximum value of S2(\($m), k) =",
first(
foreach range(1;$m+1) as $k ({computed:{}, previous: 0};
stirling2($m; $k) as $current
| if ($current.result > .previous)
then .previous = $current.result
else
.emit = "\(.previous)\n(\(.previous|tostring|length) digits, k = \($k - 1))"
end;
select(.emit).emit) );
 
part1(12)
| .emit,
part2(100)
</syntaxhighlight>
{{out}}
<pre>
Unsigned Stirling numbers of the second kind:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
 
The maximum value of S2(100, k) =
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)
Line 410 ⟶ 975:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
const s2cache = Dict()
Line 448 ⟶ 1,013:
println("\nThe maximum for stirling2(100, _) is: ", maximum(k-> stirlings2(BigInt(100), BigInt(k)), 1:100))
 
</langsyntaxhighlight>{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 10 11 12
Line 468 ⟶ 1,033:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">import java.math.BigInteger
 
fun main() {
println("Stirling numbers of the second kind:")
val max = 12
print("n/k")
for (n in 0..max) {
print("%10d".format(n))
}
println()
for (n in 0..max) {
print("%-3d".format(n))
for (k in 0..n) {
print("%10s".format(sterling2(n, k)))
}
println()
}
println("The maximum value of S2(100, k) = ")
var previous = BigInteger.ZERO
for (k in 1..100) {
val current = sterling2(100, k)
previous = if (current > previous) {
current
} else {
println("%s%n(%d digits, k = %d)".format(previous, previous.toString().length, k - 1))
break
}
}
}
 
private val COMPUTED: MutableMap<String, BigInteger> = HashMap()
private fun sterling2(n: Int, k: Int): BigInteger {
val key = "$n,$k"
if (COMPUTED.containsKey(key)) {
return COMPUTED[key]!!
}
if (n == 0 && k == 0) {
return BigInteger.valueOf(1)
}
if (n > 0 && k == 0 || n == 0 && k > 0) {
return BigInteger.ZERO
}
if (n == k) {
return BigInteger.valueOf(1)
}
if (k > n) {
return BigInteger.ZERO
}
 
val result = BigInteger.valueOf(k.toLong()) * sterling2(n - 1, k) + sterling2(n - 1, k - 1)
COMPUTED[key] = result
return result
}</syntaxhighlight>
{{out}}
<pre>Stirling numbers of the second kind:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
The maximum value of S2(100, k) =
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- show some Stirling numbers of the second kind
local MAX_STIRLING = 12;
-- construct a matrix of Stirling numbers up to max n, max n
local s2 = {}
for n = 0, MAX_STIRLING do
s2[ n ] = {}
for k = 0, MAX_STIRLING do s2[ n ][ k ] = 0 end
end
for n = 0, MAX_STIRLING do s2[ n ][ n ] = 1 end
for n = 0, MAX_STIRLING - 1 do
for k = 1, n do
s2[ n + 1 ][ k ] = k * s2[ n ][ k ] + s2[ n ][ k - 1 ]
end
end
io.write( "Stirling numbers of the second kind:\n" )
io.write( " k" )
for k = 0, MAX_STIRLING do io.write( string.format( "%8d", k ) ) end
io.write( "\n" )
io.write( " n\n" );
for n = 0, MAX_STIRLING do
io.write( string.format( "%2d", n ) )
for k = 0, n do io.write( string.format( "%8d", s2[ n ][ k ] ) ) end
io.write( "\n" )
end
end
</syntaxhighlight>
{{out}}
<pre>
Stirling numbers of the second kind:
k 0 1 2 3 4 5 6 7 8 9 10 11 12
n
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">TableForm[Array[StirlingS2, {n = 12, k = 12} + 1, {0, 0}], TableHeadings -> {"n=" <> ToString[#] & /@ Range[0, n], "k=" <> ToString[#] & /@ Range[0, k]}]
Max[Abs[StirlingS2[100, #]] & /@ Range[0, 100]]</syntaxhighlight>
{{out}}
<pre> k=0 k=1 k=2 k=3 k=4 k=5 k=6 k=7 k=8 k=9 k=10 k=11 k=12
n=0 1 0 0 0 0 0 0 0 0 0 0 0 0
n=1 0 1 0 0 0 0 0 0 0 0 0 0 0
n=2 0 1 1 0 0 0 0 0 0 0 0 0 0
n=3 0 1 3 1 0 0 0 0 0 0 0 0 0
n=4 0 1 7 6 1 0 0 0 0 0 0 0 0
n=5 0 1 15 25 10 1 0 0 0 0 0 0 0
n=6 0 1 31 90 65 15 1 0 0 0 0 0 0
n=7 0 1 63 301 350 140 21 1 0 0 0 0 0
n=8 0 1 127 966 1701 1050 266 28 1 0 0 0 0
n=9 0 1 255 3025 7770 6951 2646 462 36 1 0 0 0
n=10 0 1 511 9330 34105 42525 22827 5880 750 45 1 0 0
n=11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1 0
n=12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674</pre>
 
=={{header|Nim}}==
As for Stirling numbers of first kind, a simple program using recursive definition is enough to solve the task when not using big numbers.
<syntaxhighlight lang="nim">import sequtils, strutils
 
proc s2(n, k: Natural): Natural =
if n == k: return 1
if n == 0 or k == 0: return 0
k * s2(n - 1, k) + s2(n - 1, k - 1)
 
echo " k ", toSeq(0..12).mapIt(($it).align(2)).join(" ")
echo " n"
for n in 0..12:
stdout.write ($n).align(2)
for k in 0..n:
stdout.write ($s2(n, k)).align(8)
stdout.write '\n'</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 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1</pre>
 
Now, to solve the second part of the task, we have to use big numbers. As for Stirling numbers of first kind, we also use a cache to avoid to repeat multiple times the same computations.
{{libheader|bignum}}
<syntaxhighlight lang="nim">import tables
import bignum
 
var cache: Table[(Natural, Natural), Int]
 
proc s2(n, k: Natural): Int =
if n == k: return newInt(1)
if n == 0 or k == 0: return newInt(0)
if (n, k) in cache: return cache[(n, k)]
result = k * s2(n - 1, k) + s2(n - 1, k - 1)
cache[(n, k)] = result
 
var max = newInt(-1)
for k in 0..100:
let s = s2(100, k)
if s > max: max = s
else: break
 
echo "Maximum Stirling number of the second kind with n = 100:"
echo max</syntaxhighlight>
 
{{out}}
<pre>Maximum Stirling number of the second kind with n = 100:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use bigint;
Line 501 ⟶ 1,270:
 
say "\nMaximum value from the S2(100, *) row:";
say max map { Stirling2(101,$_) } 0..100;</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling2 numbers of the second kind: S2(n, k):
Line 523 ⟶ 1,292:
 
=={{header|Phix}}==
{{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 s2 = 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;">s2</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>
s2[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(s2[n][n],1)
<span style="color: #000000;">s2</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>
end for
<span style="color: #7060A8;">mpz_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s2</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 {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_set_si(t,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: #008080;">do</span>
mpz_mul(t,t,s2[n][k+1])
<span style="color: #7060A8;">mpz_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
mpz_add(s2[n+1][k+1],t,s2[n][k])
<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;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s2</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>
end for
<span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s2</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;">s2</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>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"Stirling numbers of the second kind: S2(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;">"Stirling numbers of the second kind: S2(n, k):\n n k:"</span><span style="color: #0000FF;">)</span>
printf(1,"%5d ", 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;">"%5d "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
printf(1,"\n--- %s\n",repeat('-',last*10+5))
<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;">10</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</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,"%9Zd ", s2[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;">"%9s "</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s2</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 s100k = s2[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(s100k,m100) > 0 then
<span style="color: #004080;">mpz</span> <span style="color: #000000;">s100k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s2</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,s100k)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s100k</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;">s100k</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"\nThe maximum S2(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 S2(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 586 ⟶ 1,358:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">:- dynamic stirling2_cache/3.
 
stirling2(N, N, 1):-!.
Line 619 ⟶ 1,391:
print_stirling_numbers_up_to(_).
 
max_stirling2(N, MMax):-
findall aggregate_all(max(L), (between(1, N, K), stirling2(N, K, L)), ListMax),.
max_list(List, M).
 
main:-
Line 628 ⟶ 1,399:
writeln('Maximum value of S2(n,k) where n = 100:'),
max_stirling2(100, M),
writeln(M).</langsyntaxhighlight>
 
{{out}}
Line 651 ⟶ 1,422:
=={{header|Python}}==
{{trans|Java}}
<syntaxhighlight lang="python">
<lang Python>
computed = {}
 
Line 691 ⟶ 1,462:
print("{0}\n({1} digits, k = {2})\n".format(previous, len(str(previous)), k - 1))
break
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 712 ⟶ 1,483:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ dip number$
over size -
space swap of
swap join echo$ ] is justify ( n n --> )
 
[ table ] is s2table ( n --> n )
 
[ swap 101 * + s2table ] is s2 ( n n --> n )
 
101 times
[ i^ 101 times
[ dup i^
[ 2dup = iff
[ 2drop 1 ] done
over 0 =
over 0 = or iff
[ 2drop 0 ] done
dip [ 1 - ]
2dup tuck s2 *
unrot 1 - s2 + ]
' s2table put ]
drop ]
cr cr
13 times
[ i^ dup 1+ times
[ dup i^ s2
10 justify ]
drop cr ]
cr
0 100 times
[ 100 i^ 1+ s2 max ]
echo cr</syntaxhighlight>
 
{{out}}
 
<pre> 1
0 1
0 1 1
0 1 3 1
0 1 7 6 1
0 1 15 25 10 1
0 1 31 90 65 15 1
0 1 63 301 350 140 21 1
0 1 127 966 1701 1050 266 28 1
0 1 255 3025 7770 6951 2646 462 36 1
0 1 511 9330 34105 42525 22827 5880 750 45 1
0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
 
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
</pre>
 
Line 718 ⟶ 1,543:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>sub Stirling2 (Int \n, Int \k) {
((1,), { (0, |@^last) »+« (|(@^last »*« @^last.keys), 0) } … *)[n;k]
}
Line 735 ⟶ 1,560:
 
say "\nMaximum value from the S2(100, *) row:";
say (^100).map( { Stirling2 100, $_ } ).max;</langsyntaxhighlight>
{{out}}
<pre>Stirling numbers of the second kind: S2(n, k):
Line 758 ⟶ 1,583:
=={{header|REXX}}==
Some extra code was added to minimize the displaying of the column widths.
<syntaxhighlight lang="text">/*REXX program to compute and display Stirling numbers of the second kind. */
parse arg lim . /*obtain optional argument from the CL.*/
if lim=='' | lim=="," then lim= 12 /*Not specified? Then use the default.*/
Line 765 ⟶ 1,590:
numeric digits max(9, 2*lim) /*(over) specify maximum number in grid*/
@.=
do j=0 for lim+1
@.j.j = 1; if j==0 then iterate /*define the right descending diagonal.*/
@.0.j = 0; @.j.0 = 0 /* " " zero values. */
end /*j*/
max#.= 0 /* [↓] calculate values for the grid. */
do n=0 for lim+1; np= n + 1
do k=1 for lim; km= k - 1
@.np.k = k * @.n.k + @.n.km /*calculate a number in the grid. */
max#.k= max(max#.k, @.n.k) /*find the maximum value for a column. */
max#.b= max(max#.b, @.n.k) /*find the maximum value for all rows. */
end /*k*/
end /*n*/
/* [↓] only show the maximum value ? */
do k=0 for lim+1 /*find max column width for each column*/
Line 787 ⟶ 1,612:
exit /*stick a fork in it, we're all done. */
end
wiwgi= max(35, length(lim+1) ) /*the maximum width of the grid's index*/
say 'row═row═' center('"columns'", max(9, max#.a + lim), '═') /*display header of the grid.*/
 
do r=0 for lim+1; $= $= /* [↓] display the grid to the term. */
do c=0 for lim+1 until c>=r /*build a row of grid, 1 col at a time.*/
$= $ right(@.r.c, length(max#.c) ) /*append a column to a row of the grid.*/
end /*c*/
say rightcenter(r,wi wgi) strip( substr($,2), 'T') /*display a single row of the grid. */
end /*r*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
row═row═ ══════════════════════════════columns══════════════════════════════
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
</pre>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> -100 </tt>}}
Line 817 ⟶ 1,642:
The maximum value (which has 115 decimal digits):
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
</pre>
 
=={{header|RPL}}==
{{works with|RPL|HP49-C}}
« '''IF''' DUP2 AND NOT '''THEN''' ==
'''ELSE'''
SWAP 1 - OVER
DUP2 1 - <span style="color:blue">S2</span> 4 ROLLD <span style="color:blue">S2</span> * +
'''END'''
» '<span style="color:blue">S2</span>' STO <span style="color:grey">''@ ( n k → S2(n,k) )''</span>
 
12 12 « <span style="color:blue">S2</span> » LCXM
{{out}}
<pre>
1: [[ 1 0 0 0 0 0 0 0 0 0 0 0 ]
[ 1 1 0 0 0 0 0 0 0 0 0 0 ]
[ 1 3 1 0 0 0 0 0 0 0 0 0 ]
[ 1 7 6 1 0 0 0 0 0 0 0 0 ]
[ 1 15 25 10 1 0 0 0 0 0 0 0 ]
[ 1 31 90 65 15 1 0 0 0 0 0 0 ]
[ 1 63 301 350 140 21 1 0 0 0 0 0 ]
[ 1 127 966 1701 1050 266 28 1 0 0 0 0 ]
[ 1 255 3025 7770 6951 2646 462 36 1 0 0 0 ]
[ 1 511 9330 34105 42525 22827 5880 750 45 1 0 0 ]
[ 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1 0 ]
[ 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1 ]]
</pre>
 
=={{header|Ruby}}==
 
<syntaxhighlight lang="ruby">@memo = {}
 
def sterling2(n, k)
key = [n,k]
return @memo[key] if @memo.key?(key)
return 1 if n.zero? and k.zero?
return 0 if n.zero? or k.zero?
return 1 if n == k
return 0 if k > n
res = k * sterling2(n-1, k) + sterling2(n - 1, k-1)
@memo[key] = res
end
 
r = (0..12)
puts "Sterling2 numbers:"
puts "n/k #{r.map{|n| "%11d" % n}.join}"
r.each do |row|
print "%-4s" % row
puts "#{(0..row).map{|col| "%11d" % sterling2(row, col)}.join}"
end
puts "\nMaximum value from the sterling2(100, k)";
puts (1..100).map{|a| sterling2(100,a)}.max
</syntaxhighlight>
{{out}}<pre>Sterling2 numbers:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
 
Maximum value from the L(100, *) row:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
 
</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func S2(n, k) { # Stirling numbers of the second kind
stirling2(n, k)
}
Line 840 ⟶ 1,739:
say "\nMaximum value from the S2(#{n}, *) row:"
say { S2(n, _) }.map(^n).max
}</langsyntaxhighlight>
{{out}}
<pre>
Line 863 ⟶ 1,762:
 
Alternatively, the '''S2(n,k)''' function can be defined as:
<langsyntaxhighlight lang="ruby">func S2((0), (0)) { 1 }
func S2(_, (0)) { 0 }
func S2((0), _) { 0 }
func S2(n, k) is cached { S2(n-1, k)*k + S2(n-1, k-1) }</langsyntaxhighlight>
 
=={{header|Tcl}}==
 
{{trans|Java}}
<langsyntaxhighlight Tcllang="tcl">proc S2 {n k} {
set nk [list $n $k]
if {[info exists ::S2cache($nk)]} {
Line 920 ⟶ 1,819:
}
}
main</langsyntaxhighlight>
{{out}}
<pre>
Line 939 ⟶ 1,838:
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
The maximum value of S2(100, k) =
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|Java}}
<syntaxhighlight lang="vbnet">Imports System.Numerics
 
Module Module1
 
Class Sterling
Private Shared ReadOnly COMPUTED As New Dictionary(Of String, BigInteger)
 
Private Shared Function CacheKey(n As Integer, k As Integer) As String
Return String.Format("{0}:{1}", n, k)
End Function
 
Private Shared Function Impl(n As Integer, k As Integer) As BigInteger
If n = 0 AndAlso k = 0 Then
Return 1
End If
If (n > 0 AndAlso k = 0) OrElse (n = 0 AndAlso k > 0) Then
Return 0
End If
If n = k Then
Return 1
End If
If k > n Then
Return 0
End If
 
Return k * Sterling2(n - 1, k) + Sterling2(n - 1, k - 1)
End Function
 
Public Shared Function Sterling2(n As Integer, k As Integer) As BigInteger
Dim key = CacheKey(n, k)
If COMPUTED.ContainsKey(key) Then
Return COMPUTED(key)
End If
 
Dim result = Impl(n, k)
COMPUTED.Add(key, result)
Return result
End Function
End Class
 
Sub Main()
Console.WriteLine("Stirling numbers of the second kind:")
Dim max = 12
Console.Write("n/k")
For n = 0 To max
Console.Write("{0,10}", n)
Next
Console.WriteLine()
For n = 0 To max
Console.Write("{0,3}", n)
For k = 0 To n
Console.Write("{0,10}", Sterling.Sterling2(n, k))
Next
Console.WriteLine()
Next
Console.WriteLine("The maximum value of S2(100, k) = ")
Dim previous = BigInteger.Zero
For k = 1 To 100
Dim current = Sterling.Sterling2(100, k)
If current > previous Then
previous = current
Else
Console.WriteLine(previous)
Console.WriteLine("({0} digits, k = {1})", previous.ToString().Length, k - 1)
Exit For
End If
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>Stirling numbers of the second kind:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
The maximum value of S2(100, k) =
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)</pre>
 
=={{header|Wren}}==
{{trans|Java}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./big" for BigInt
import "./fmt" for Fmt
 
var computed = {}
 
var stirling2 // recursive
stirling2 = Fn.new { |n, k|
var key = "%(n),%(k)"
if (computed.containsKey(key)) return computed[key]
if (n == 0 && k == 0) return BigInt.one
if ((n > 0 && k == 0) || (n == 0 && k > 0)) return BigInt.zero
if (k == n) return BigInt.one
if (k > n) return BigInt.zero
var result = stirling2.call(n-1, k-1) + stirling2.call(n-1, k)*k
computed[key] = result
return result
}
 
System.print("Unsigned Stirling numbers of the second kind:")
var max = 12
System.write("n/k")
for (n in 0..max) Fmt.write("$10d", n)
System.print()
for (n in 0..max) {
Fmt.write("$-3d", n)
for (k in 0..n) Fmt.write("$10i", stirling2.call(n, k))
System.print()
}
System.print("The maximum value of S2(100, k) =")
var previous = BigInt.zero
for (k in 1..100) {
var current = stirling2.call(100, k)
if (current > previous) {
previous = current
} else {
Fmt.print("$i\n($d digits, k = $d)", previous, previous.toString.count, k - 1)
break
}
}</syntaxhighlight>
 
{{out}}
<pre>
Unsigned Stirling numbers of the second kind:
n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
The maximum value of S2(100, k) =
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)
Line 944 ⟶ 2,001:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn stirling2(n,k){
var seen=Dictionary(); // cache for recursion
if(n==k) return(1); // (0.0)==1
Line 952 ⟶ 2,009:
if(Void==(s2 := seen.find(z2))){ s2 = seen[z2] = stirling2(n-1,k-1) }
k*s1 + s2; // k is first to cast to BigInt (if using BigInts)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">// calculate entire table (cached), find max, find num digits in max
N,mx := 12, [1..N].apply(fcn(n){ [1..n].apply(stirling2.fp(n)) }).flatten() : (0).max(_);
fmt:="%%%dd".fmt("%d".fmt(mx.numDigits + 1)).fmt; // "%9d".fmt
Line 960 ⟶ 2,017:
foreach row in ([0..N]){
println("%3d".fmt(row), [0..row].pump(String, stirling2.fp(row), fmt));
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 980 ⟶ 2,037:
</pre>
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
N=100;
S2100:=[BI(2)..N].apply(stirling2.fp(BI(N))).reduce(fcn(m,n){ m.max(n) });
println("Maximum value from the S2(%d,*) row (%d digits):".fmt(N,S2100.numDigits));
println(S2100);</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
1,150

edits