Stirling numbers of the first kind: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Quackery}}: added stack comments)
Line 1,394: Line 1,394:
swap join echo$ ] is justify ( n n --> )
swap join echo$ ] is justify ( n n --> )


[ table ] is s1table
[ table ] is s1table ( n --> n )

[ swap 101 * + s1table ] is s1
[ swap 101 * + s1table ] is s1 ( n n --> n )


101 times
101 times

Revision as of 09:40, 2 July 2021

Task
Stirling numbers of the first kind
You are encouraged to solve this task according to the task description, using any language you may know.

Stirling numbers of the first kind, or Stirling cycle numbers, count permutations according to their number of cycles (counting fixed points as cycles of length one).

They may be defined directly to be the number of permutations of n elements with k disjoint cycles.

Stirling numbers of the first kind express coefficients of polynomial expansions of falling or rising factorials.

Depending on the application, Stirling numbers of the first kind may be "signed" or "unsigned". Signed Stirling numbers of the first kind arise when the polynomial expansion is expressed in terms of falling factorials; unsigned when expressed in terms of rising factorials. The only substantial difference is that, for signed Stirling numbers of the first kind, values of S1(n, k) are negative when n + k is odd.

Stirling numbers of the first kind follow the simple identities:

   S1(0, 0) = 1
   S1(n, 0) = 0 if n > 0
   S1(n, k) = 0 if k > n
   S1(n, k) = S1(n - 1, k - 1) + (n - 1) * S1(n - 1, k) # For unsigned
     or
   S1(n, k) = S1(n - 1, k - 1) - (n - 1) * S1(n - 1, k) # For signed


Task
  • Write a routine (function, procedure, whatever) to find Stirling numbers of the first kind. There are several methods to generate Stirling numbers of the first kind. You are free to choose the most appropriate for your language. If your language has a built-in, or easily, publicly available library implementation, it is acceptable to use that.
  • Using the routine, generate and show here, on this page, a table (or triangle) showing the Stirling numbers of the first kind, S1(n, k), up to S1(12, 12). it is optional to show the row / column for n == 0 and k == 0. It is optional to show places where S1(n, k) == 0 (when k > n). You may choose to show signed or unsigned Stirling numbers of the first kind, just make a note of which was chosen.
  • If your language supports large integers, find and show here, on this page, the maximum value of S1(n, k) where n == 100.


See also


Related Tasks



11l

Translation of: Python

<lang 11l>[(Int, Int) = BigInt] computed

F sterling1(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
     R BigInt(0)
  I k > n
     R BigInt(0)
  V result = sterling1(n - 1, k - 1) + (n - 1) * sterling1(n - 1, k)
  :computed[key] = result
  R result

print(‘Unsigned Stirling numbers of the first 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(sterling1(n, k)).rjust(10), end' ‘’)
  print()

print(‘The maximum value of S1(100, k) = ’) BigInt previous = 0 L(k) 1 .. 100

  V current = sterling1(100, k)
  I current > previous
     previous = current
  E
     print("#.\n(#. digits, k = #.)\n".format(previous, String(previous).len, k - 1))
     L.break</lang>
Output:
Unsigned Stirling numbers of the first 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         2         3         1
4                  0         6        11         6         1
5                  0        24        50        35        10         1
6                  0       120       274       225        85        15         1
7                  0       720      1764      1624       735       175        21         1
8                  0      5040     13068     13132      6769      1960       322        28         1
9                  0     40320    109584    118124     67284     22449      4536       546        36         1
10                 0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11                 0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12                 0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
The maximum value of S1(100, k) = 
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)

ALGOL 68

Works with: ALGOL 68G version Any - tested with release 2.8.3.win32

Uses the Algol 68G LONG LONG INT mode which provides large-precision integers. As the default number of digits is insufficient for the task, the maximum nunber of digits is specified by a pragmatic comment. <lang algol68>BEGIN

   # show some (unsigned) Stirling numbers of the first kind            #
   # specify the precision of LONG LONG INT, we need about 160 digits   #
   # for Stirling numbers of the first kind with n, k = 100             #
   PR precision 160 PR
   MODE SINT = LONG LONG INT;
   # returns a triangular matrix of Stirling numbers up to max n, max n #
   # the numbers are signed if signed is TRUE, unsigned otherwise       #
   PROC make s1 = ( INT max n, BOOL signed )REF[,]SINT:
   BEGIN
       REF[,]SINT s1 := HEAP[ 0 : max n, 0 : max n ]SINT;
       FOR n FROM 0 TO max n DO FOR k FROM 0 TO max n DO s1[ n, k ] := 0 OD OD;
       s1[ 0, 0 ] := 1;
       FOR n FROM 1 TO max n DO s1[ n, 0 ] := 0 OD;
       FOR n FROM 1 TO max n DO
           FOR k FROM 1 TO n DO
               SINT s1 term = ( ( n - 1 ) * s1[ n - 1, k ] );
               s1[ n, k ] := s1[ n - 1, k - 1 ] + IF signed THEN - s1 term ELSE s1 term FI
           OD
       OD;
       s1
   END # make s1 # ;
   # task requirements:                                                #
   # print Stirling numbers up to n, k = 12                            #
   BEGIN
       INT max stirling = 12;
       REF[,]SINT s1 = make s1( max stirling, FALSE );
       print( ( "Unsigned Stirling numbers of the first kind:", newline ) );
       print( ( " k" ) );
       FOR k FROM 0 TO max stirling DO print( ( whole( k, -10 ) ) ) 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( s1[ n, k ], -10 ) ) )
           OD;
           print( ( newline ) )
       OD
   END;
   # find the maximum Stirling number with n = 100                     #
   BEGIN
       INT max stirling = 100;
       REF[,]SINT s1 = make s1( max stirling, FALSE );
       SINT max 100 := 0;
       FOR k FROM 0 TO max stirling DO
           IF s1[ max stirling, k ] > max 100 THEN max 100 := s1[ max stirling, k ] FI
       OD;
       print( ( "Maximum Stirling number of the first kind with n = 100:", newline ) );
       print( ( whole( max 100, 0 ), newline ) )
   END

END</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
 4         0         6        11         6         1
 5         0        24        50        35        10         1
 6         0       120       274       225        85        15         1
 7         0       720      1764      1624       735       175        21         1
 8         0      5040     13068     13132      6769      1960       322        28         1
 9         0     40320    109584    118124     67284     22449      4536       546        36         1
10         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
Maximum Stirling number of the first kind with n = 100:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

ALGOL W

<lang algolw>begin % show some (unsigned) Stirling numbers of the first kind  %

   integer MAX_STIRLING;
   MAX_STIRLING := 12;
   begin
       % construct a matrix of Stirling numbers up to max n, max n           %
       integer array s1 ( 0 :: MAX_STIRLING, 0 :: MAX_STIRLING );
       for n := 0 until MAX_STIRLING do begin
           for k := 0 until MAX_STIRLING do s1( n, k ) := 0
       end for_n ;
       s1( 0, 0 ) := 1;
       for n := 1 until MAX_STIRLING do s1( n, 0 ) := 0;
       for n := 1 until MAX_STIRLING do begin
           for k := 1 until n do begin
               integer s1Term;
               s1Term := ( ( n - 1 ) * s1( n - 1, k ) );
               s1( n, k ) := s1( n - 1, k - 1 ) + s1Term
           end for_k
       end for_n ;
       % print the Stirling numbers up to n, k = 12                          %
       write( "Unsigned Stirling numbers of the first kind:" );
       write( " k" );
       for k := 0 until MAX_STIRLING do writeon( i_w := 10, 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 begin
               writeon( i_w := 10, s_w := 0, s1( n, k ) )
           end for_k
       end for_n
   end

end.</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
 4         0         6        11         6         1
 5         0        24        50        35        10         1
 6         0       120       274       225        85        15         1
 7         0       720      1764      1624       735       175        21         1
 8         0      5040     13068     13132      6769      1960       322        28         1
 9         0     40320    109584    118124     67284     22449      4536       546        36         1
10         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

C

<lang c>#include <stdbool.h>

  1. include <stdio.h>
  2. include <stdlib.h>

typedef struct stirling_cache_tag {

   int max;
   int* values;

} stirling_cache;

int stirling_number1(stirling_cache* sc, int n, int k) {

   if (k == 0)
       return n == 0 ? 1 : 0;
   if (n > sc->max || k > n)
       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_number1(sc, n - 1, k - 1);
           int s2 = stirling_number1(sc, n - 1, k);
           values[n*(n-1)/2 + k - 1] = s1 + s2 * (n - 1);
       }
   }
   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("Unsigned Stirling numbers of the first kind:\nn/k");
   for (int k = 0; k <= max; ++k)
       printf(k == 0 ? "%2d" : "%10d", k);
   printf("\n");
   for (int n = 0; n <= max; ++n) {
       printf("%2d ", n);
       for (int k = 0; k <= n; ++k)
           printf(k == 0 ? "%2d" : "%10d", stirling_number1(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;

}</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
 4  0         6        11         6         1
 5  0        24        50        35        10         1
 6  0       120       274       225        85        15         1
 7  0       720      1764      1624       735       175        21         1
 8  0      5040     13068     13132      6769      1960       322        28         1
 9  0     40320    109584    118124     67284     22449      4536       546        36         1
10  0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11  0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12  0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

C++

Library: GMP

<lang cpp>#include <algorithm>

  1. include <iomanip>
  2. include <iostream>
  3. include <map>
  4. include <gmpxx.h>

using integer = mpz_class;

class unsigned_stirling1 { public:

   integer get(int n, int k);

private:

   std::map<std::pair<int, int>, integer> cache_;

};

integer unsigned_stirling1::get(int n, int k) {

   if (k == 0)
       return n == 0 ? 1 : 0;
   if (k > n)
       return 0;
   auto p = std::make_pair(n, k);
   auto i = cache_.find(p);
   if (i != cache_.end())
       return i->second;
   integer s = get(n - 1, k - 1) + (n - 1) * get(n - 1, k);
   cache_.emplace(p, s);
   return s;

}

void print_stirling_numbers(unsigned_stirling1& s1, int n) {

   std::cout << "Unsigned Stirling numbers of the first kind:\nn/k";
   for (int j = 0; j <= n; ++j) {
       std::cout << std::setw(j == 0 ? 2 : 10) << 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 : 10) << s1.get(i, j);
       std::cout << '\n';
   }

}

int main() {

   unsigned_stirling1 s1;
   print_stirling_numbers(s1, 12);
   std::cout << "Maximum value of S1(n,k) where n == 100:\n";
   integer max = 0;
   for (int k = 0; k <= 100; ++k)
       max = std::max(max, s1.get(100, k));
   std::cout << max << '\n';
   return 0;

}</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
 4  0         6        11         6         1
 5  0        24        50        35        10         1
 6  0       120       274       225        85        15         1
 7  0       720      1764      1624       735       175        21         1
 8  0      5040     13068     13132      6769      1960       322        28         1
 9  0     40320    109584    118124     67284     22449      4536       546        36         1
10  0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11  0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12  0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
Maximum value of S1(n,k) where n == 100:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

D

Translation of: Java

<lang d>import std.bigint; import std.functional; import std.stdio;

alias sterling1 = memoize!sterling1Impl; BigInt sterling1Impl(int n, int k) {

   if (n == 0 && k == 0) {
       return BigInt(1);
   }
   if (n > 0 && k == 0) {
       return BigInt(0);
   }
   if (k > n) {
       return BigInt(0);
   }
   return sterling1(n - 1, k - 1) + (n - 1) * sterling1(n - 1, k);

}

void main() {

   writeln("Unsigned Stirling numbers of the first kind:");
   int max = 12;
   write("n/k");
   foreach (n; 0 .. max + 1) {
       writef("%10d", n);
   }
   writeln;
   foreach (n; 0 .. max + 1) {
       writef("%-3d", n);
       foreach (k; 0 .. n + 1) {
           writef("%10s", sterling1(n, k));
       }
       writeln;
   }
   writeln("The maximum value of S1(100, k) = ");
   auto previous = BigInt(0);
   foreach (k; 1 .. 101) {
       auto current = sterling1(100, k);
       if (previous < current) {
           previous = current;
       } else {
           import std.conv;
           writeln(previous);
           writefln("(%d digits, k = %d)", previous.to!string.length, k - 1);
           break;
       }
   }

}</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
4           0         6        11         6         1
5           0        24        50        35        10         1
6           0       120       274       225        85        15         1
7           0       720      1764      1624       735       175        21         1
8           0      5040     13068     13132      6769      1960       322        28         1
9           0     40320    109584    118124     67284     22449      4536       546        36         1
10          0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11          0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12          0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
The maximum value of S1(100, k) =
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)

Factor

Here we calculate a row at a time as the coefficients of the falling factorial x(x-1)(x-2)...(x-n+1) using Factor's built-in polynomial arithmetic.

For example, x(x-1)(x-2) = x3 - 3x2 + 2x. Taking the absolute values of the coefficients, the third row is (0) 2 3 1.

Works with: Factor version 0.99 development version 2019-07-10

<lang factor>USING: arrays assocs formatting io kernel math math.polynomials math.ranges prettyprint sequences ; IN: rosetta-code.stirling-first

stirling-row ( n -- seq )
   [ { 1 } ] [
       [ -1 ] dip neg [a,b) dup length 1 <array> zip
       { 0 1 } [ p* ] reduce [ abs ] map
   ] if-zero ;

"Unsigned Stirling numbers of the first kind:" print "n\\k" write 13 dup [ "%10d" printf ] each-integer nl

[ dup "%-2d " printf stirling-row [ "%10d" printf ] each nl ] each-integer nl

"Maximum value from 100th stirling row:" print 100 stirling-row supremum .</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
4           0         6        11         6         1
5           0        24        50        35        10         1
6           0       120       274       225        85        15         1
7           0       720      1764      1624       735       175        21         1
8           0      5040     13068     13132      6769      1960       322        28         1
9           0     40320    109584    118124     67284     22449      4536       546        36         1
10          0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11          0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12          0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

Maximum value from 100th stirling row:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

FreeBASIC

<lang freebasic>dim as integer S1(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

S1(0, 0) = 1

for n = 0 to 12 'calculate table

   for k = 1 to n
       S1(n, k) = S1(n-1, k-1) - (n-1) * S1(n-1, k)
   next k

next n

print "Signed Stirling numbers of the first 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, S1(n, k))
   next k
   print outstr

next n</lang>

Signed Stirling numbers of the first 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           2          -3           1           0           0           0           0           0           0           0           0           0
 4             0          -6          11          -6           1           0           0           0           0           0           0           0           0
 5             0          24         -50          35         -10           1           0           0           0           0           0           0           0
 6             0        -120         274        -225          85         -15           1           0           0           0           0           0           0
 7             0         720       -1764        1624        -735         175         -21           1           0           0           0           0           0
 8             0       -5040       13068      -13132        6769       -1960         322         -28           1           0           0           0           0
 9             0       40320     -109584      118124      -67284       22449       -4536         546         -36           1           0           0           0
10             0     -362880     1026576    -1172700      723680     -269325       63273       -9450         870         -45           1           0           0
11             0     3628800   -10628640    12753576    -8409500     3416930     -902055      157773      -18150        1320         -55           1           0
12             0   -39916800   120543840  -150917976   105258076   -45995730    13339535    -2637558      357423      -32670        1925         -66           1

Fōrmulæ

In this page you can see the solution of this task.

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text (more info). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.

Go

<lang go>package main

import (

   "fmt"
   "math/big"

)

func main() {

   limit := 100
   last := 12
   unsigned := true
   s1 := make([][]*big.Int, limit+1)
   for n := 0; n <= limit; n++ {
       s1[n] = make([]*big.Int, limit+1)
       for k := 0; k <= limit; k++ {
           s1[n][k] = new(big.Int)
       }
   }
   s1[0][0].SetInt64(int64(1))
   var t big.Int
   for n := 1; n <= limit; n++ {
       for k := 1; k <= n; k++ {
           t.SetInt64(int64(n - 1))
           t.Mul(&t, s1[n-1][k])            
           if unsigned {
               s1[n][k].Add(s1[n-1][k-1], &t)
           } else {
               s1[n][k].Sub(s1[n-1][k-1], &t)
           }           
       }
   }
   fmt.Println("Unsigned Stirling numbers of the first kind: S1(n, k):")
   fmt.Printf("n/k")
   for i := 0; i <= last; i++ {
       fmt.Printf("%9d ", i)
   }
   fmt.Printf("\n--")
   for i := 0; i <= last; i++ {
       fmt.Printf("----------")
   }
   fmt.Println()
   for n := 0; n <= last; n++ {
       fmt.Printf("%2d ", n)
       for k := 0; k <= n; k++ {
           fmt.Printf("%9d ", s1[n][k])
       }
       fmt.Println()
   }
   fmt.Println("\nMaximum value from the S1(100, *) row:")
   max := new(big.Int).Set(s1[limit][0])
   for k := 1; k <= limit; k++ {
       if s1[limit][k].Cmp(max) > 0 {
           max.Set(s1[limit][k])
       }
   }
   fmt.Println(max)
   fmt.Printf("which has %d digits.\n", len(max.String()))

}</lang>

Output:
Unsigned Stirling numbers of the first kind: S1(n, k):
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         2         3         1 
 4         0         6        11         6         1 
 5         0        24        50        35        10         1 
 6         0       120       274       225        85        15         1 
 7         0       720      1764      1624       735       175        21         1 
 8         0      5040     13068     13132      6769      1960       322        28         1 
 9         0     40320    109584    118124     67284     22449      4536       546        36         1 
10         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1 
11         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1 
12         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1 

Maximum value from the S1(100, *) row:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
which has 158 digits.

Haskell

Using library: data-memocombinators for memoization <lang haskell>import Text.Printf (printf) import Data.List (groupBy) import qualified Data.MemoCombinators as Memo

stirling1 :: Integral a => (a, a) -> a stirling1 = Memo.pair Memo.integral Memo.integral f

 where
   f (n, k)
     | n == 0 && k == 0 = 1
     |  n > 0 && k == 0 = 0
     | k > n            = 0
     | otherwise = stirling1 (pred n, pred k) + pred n * stirling1 (pred n, 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" . stirling1) row >> printf "\n") table
 printf "\nThe maximum value of S1(100, k):\n%d\n" $
   maximum ([stirling1 (100, n) | n <- [1..100]] :: [Integer])
 where
   table :: (Int, Int)
   table = groupBy (\a b -> fst a == fst b) $ (,) <$> [0..12] <*> [0..12]</lang>

Or library: monad-memo for memoization. <lang haskell>{-# LANGUAGE FlexibleContexts #-}

import Text.Printf (printf) import Data.List (groupBy) import Control.Monad.Memo (MonadMemo, memo, startEvalMemo)

stirling1 :: (Integral n, MonadMemo (n, n) n m) => (n, n) -> m n stirling1 (n, k)

 | n == 0 && k == 0 = pure 1
 | n > 0 && k == 0 = pure 0
 | k > n           = pure 0
 | otherwise = (\f1 f2 -> f1 + pred n * f2) <$>
     memo stirling1 (pred n, pred k) <*> memo stirling1 (pred n, k)

stirling1Memo :: Integral n => (n, n) -> n stirling1Memo = startEvalMemo . stirling1

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" . stirling1Memo) row >> printf "\n") table
 printf "\nThe maximum value of S1(100, k):\n%d\n" $
   maximum ([stirling1Memo (100, n) | n <- [1..100]] :: [Integer])
 where
   table :: (Int, Int)
   table = groupBy (\a b -> fst a == fst b) $ (,) <$> [0..12] <*> [0..12]</lang>
Output:
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         2         3         1         0         0         0         0         0         0         0         0         0
 4|         0         6        11         6         1         0         0         0         0         0         0         0         0
 5|         0        24        50        35        10         1         0         0         0         0         0         0         0
 6|         0       120       274       225        85        15         1         0         0         0         0         0         0
 7|         0       720      1764      1624       735       175        21         1         0         0         0         0         0
 8|         0      5040     13068     13132      6769      1960       322        28         1         0         0         0         0
 9|         0     40320    109584    118124     67284     22449      4536       546        36         1         0         0         0
10|         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1         0         0
11|         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1         0
12|         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

The maximum value of S1(100, k):
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

J

   NB. agenda set by the test according to the definition

   test=: 1 i.~ (0 0&-: , 1 0&-:)@:*@:, , <
   s1=: 1:`0:`0:`($:&<: + (($: * [)~ <:)~)@.test

   s1&> table i. 13
+----+------------------------------------------------------------------------------------------+
|s1&>|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        2         3         1         0        0        0       0      0     0    0  0  0|
| 4  |0        6        11         6         1        0        0       0      0     0    0  0  0|
| 5  |0       24        50        35        10        1        0       0      0     0    0  0  0|
| 6  |0      120       274       225        85       15        1       0      0     0    0  0  0|
| 7  |0      720      1764      1624       735      175       21       1      0     0    0  0  0|
| 8  |0     5040     13068     13132      6769     1960      322      28      1     0    0  0  0|
| 9  |0    40320    109584    118124     67284    22449     4536     546     36     1    0  0  0|
|10  |0   362880   1026576   1172700    723680   269325    63273    9450    870    45    1  0  0|
|11  |0  3628800  10628640  12753576   8409500  3416930   902055  157773  18150  1320   55  1  0|
|12  |0 39916800 120543840 150917976 105258076 45995730 13339535 2637558 357423 32670 1925 66  1|
+----+------------------------------------------------------------------------------------------+


   timespacex 's1&> table i. 13'
0.0242955 12928

   NB. memoization greatly helps execution time

   s1M=: 1:`0:`0:`($:&<: + (($: * [)~ <:)~)@.test M.
   timespacex 's1M&> table i. 13'
0.000235206 30336

   NB. third task
   >./100 s1M&x:&> i.101
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

Java

<lang java> import java.math.BigInteger; import java.util.HashMap; import java.util.Map;

public class SterlingNumbersFirstKind {

   public static void main(String[] args) {
       System.out.println("Unsigned Stirling numbers of the first kind:");
       int max = 12;
       System.out.printf("n/k");
       for ( int n = 0 ; n <= max ; n++ ) {
           System.out.printf("%10d", n);
       }
       System.out.printf("%n");
       for ( int n = 0 ; n <= max ; n++ ) {
           System.out.printf("%-3d", n);
           for ( int k = 0 ; k <= n ; k++ ) {
               System.out.printf("%10s", sterling1(n, k));
           }
           System.out.printf("%n");
       }
       System.out.println("The maximum value of S1(100, k) = ");
       BigInteger previous = BigInteger.ZERO;
       for ( int k = 1 ; k <= 100 ; k++ ) {
           BigInteger current = sterling1(100, k);
           if ( current.compareTo(previous) > 0 ) {
               previous = current;
           }
           else {
               System.out.printf("%s%n(%d digits, k = %d)%n", previous, previous.toString().length(), k-1);
               break;
           }
       }
   }
   
   private static Map<String,BigInteger> COMPUTED = new HashMap<>();
   
   private static final BigInteger sterling1(int n, int k) {
       String key = n + "," + k;
       if ( COMPUTED.containsKey(key) ) {
           return COMPUTED.get(key);
       }
       if ( n == 0 && k == 0 ) {
           return BigInteger.valueOf(1);
       }
       if ( n > 0 && k == 0 ) {
           return BigInteger.ZERO; 
       }
       if ( k > n ) {
           return BigInteger.ZERO;
       }
       BigInteger result = sterling1(n-1, k-1).add(BigInteger.valueOf(n-1).multiply(sterling1(n-1, k)));
       COMPUTED.put(key, result);
       return result;
   }

} </lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
4           0         6        11         6         1
5           0        24        50        35        10         1
6           0       120       274       225        85        15         1
7           0       720      1764      1624       735       175        21         1
8           0      5040     13068     13132      6769      1960       322        28         1
9           0     40320    109584    118124     67284     22449      4536       546        36         1
10          0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11          0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12          0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
The maximum value of S1(100, k) = 
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)

Julia

<lang julia>using Combinatorics

const s1cache = Dict()

function stirlings1(n, k, signed::Bool=false)

   if signed == true && isodd(n - k)
       return -stirlings1(n, k)
   elseif haskey(s1cache, Pair(n, k))
       return s1cache[Pair(n, k)]
   elseif n < 0
       throw(DomainError(n, "n must be nonnegative"))
   elseif n == k == 0
       return one(n)
   elseif n == 0 || k == 0
       return zero(n)
   elseif n == k
       return one(n)
   elseif k == 1
       return factorial(n-1)
   elseif k == n - 1
       return binomial(n, 2)
   elseif k == n - 2
       return div((3 * n - 1) * binomial(n, 3), 4)
   elseif k == n - 3
       return binomial(n, 2) * binomial(n, 4)
   end
   ret = (n - 1) * stirlings1(n - 1, k) + stirlings1(n - 1, k - 1)
   s1cache[Pair(n, k)] = ret
   return ret

end

function printstirling1table(kmax)

   println("  ", mapreduce(i -> lpad(i, 10), *, 0:kmax))
   sstring(n, k) = begin i = stirlings1(n, k); lpad(k > n && i == 0 ? "" : i, 10) end
   for n in 0:kmax
       println(rpad(n, 2) * mapreduce(k -> sstring(n, k), *, 0:kmax))
   end

end

printstirling1table(12) println("\nThe maximum for stirling1(100, _) is:\n", maximum(k-> stirlings1(BigInt(100), BigInt(k)), 1:100))

</lang>

Output:
           0         1         2         3         4         5         6         7         8         9        10        11        12
0          1
1          0         1
2          0         1         1
3          0         2         3         1
4          0         6        11         6         1
5          0        24        50        35        10         1
6          0       120       274       225        85        15         1
7          0       720      1764      1624       735       175        21         1
8          0      5040     13068     13132      6769      1960       322        28         1
9          0     40320    109584    118124     67284     22449      4536       546        36         1
10         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

The maximum for stirling1(100, _) is:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

Kotlin

Translation of: Java

<lang scala>import java.math.BigInteger

fun main() {

   println("Unsigned Stirling numbers of the first 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(sterling1(n, k)))
       }
       println()
   }
   println("The maximum value of S1(100, k) = ")
   var previous = BigInteger.ZERO
   for (k in 1..100) {
       val current = sterling1(100, k)
       previous = if (current!! > previous) {
           current
       } else {
           println("$previous\n(${previous.toString().length} digits, k = ${k - 1})")
           break
       }
   }

}

private val COMPUTED: MutableMap<String, BigInteger?> = HashMap() private fun sterling1(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) {
       return BigInteger.ZERO
   }
   if (k > n) {
       return BigInteger.ZERO
   }
   val result = sterling1(n - 1, k - 1)!!.add(BigInteger.valueOf(n - 1.toLong()).multiply(sterling1(n - 1, k)))
   COMPUTED[key] = result
   return result

}</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
4           0         6        11         6         1
5           0        24        50        35        10         1
6           0       120       274       225        85        15         1
7           0       720      1764      1624       735       175        21         1
8           0      5040     13068     13132      6769      1960       322        28         1
9           0     40320    109584    118124     67284     22449      4536       546        36         1
10          0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11          0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12          0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
The maximum value of S1(100, k) = 
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)

Mathematica / Wolfram Language

<lang Mathematica>TableForm[Array[StirlingS1, {n = 12, k = 12} + 1, {0, 0}], TableHeadings -> {"n=" <> ToString[#] & /@ Range[0, n], "k=" <> ToString[#] & /@ Range[0, k]}] Max[Abs[StirlingS1[100, #]] & /@ Range[0, 100]]</lang>

Output:
	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	2	-3	1	0	0	0	0	0	0	0	0	0
n=4	0	-6	11	-6	1	0	0	0	0	0	0	0	0
n=5	0	24	-50	35	-10	1	0	0	0	0	0	0	0
n=6	0	-120	274	-225	85	-15	1	0	0	0	0	0	0
n=7	0	720	-1764	1624	-735	175	-21	1	0	0	0	0	0
n=8	0	-5040	13068	-13132	6769	-1960	322	-28	1	0	0	0	0
n=9	0	40320	-109584	118124	-67284	22449	-4536	546	-36	1	0	0	0
n=10	0	-362880	1026576	-1172700	723680	-269325	63273	-9450	870	-45	1	0	0
n=11	0	3628800	-10628640	12753576	-8409500	3416930	-902055	157773	-18150	1320	-55	1	0
n=12	0	-39916800	120543840	-150917976	105258076	-45995730	13339535	-2637558	357423	-32670	1925	-66	1
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000


Nim

A simple implementation using the recursive definition is enough to complete the task.

<lang Nim>import sequtils, strutils

proc s1(n, k: Natural): Natural =

 if k == 0: return ord(n == 0)
 if k > n: return 0
 s1(n - 1, k - 1) + (n - 1) * s1(n - 1, k)

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 ($s1(n, k)).align(10)
 stdout.write '\n'</lang>
Output:
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         2         3         1
 4         0         6        11         6         1
 5         0        24        50        35        10         1
 6         0       120       274       225        85        15         1
 7         0       720      1764      1624       735       175        21         1
 8         0      5040     13068     13132      6769      1960       322        28         1
 9         0     40320    109584    118124     67284     22449      4536       546        36         1
10         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

To find the maximum value of S1(n, k) for n = 100, we need to use a third party library such as “bignum”. But this is not enough. To avoid multiple computation of the same terms, we have to use a cache. Note also that this method has its limits as it depends on the allowed depth of recursion. Here, this is not a problem and the result is obtained almost instantaneously.

Library: bignum

<lang Nim>import tables import bignum

var cache: Table[(Natural, Natural), Int]

proc s1(n, k: Natural): Int =

 if k == 0: return newInt(ord(n == 0))
 if k > n: return newInt(0)
 if (n, k) in cache: return cache[(n, k)]
 result = s1(n - 1, k - 1) + (n - 1) * s1(n - 1, k)
 cache[(n, k)] = result

var max = newInt(-1) for k in 0..100:

 let s = s1(100, k)
 if s > max: max = s
 else: break

echo "Maximum Stirling number of the first kind with n = 100:" echo max</lang>

Output:
Maximum Stirling number of the first kind with n = 100:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

Perl

Translation of: Raku

<lang perl>use strict; use warnings; use bigint; use feature 'say'; use feature 'state'; no warnings 'recursion'; use List::Util qw(max);

sub Stirling1 {

   my($n, $k) = @_;
   return 1 unless $n || $k;
   return 0 unless $n && $k;
   state %seen;
   return ($seen{"{$n-1}|{$k-1}"} //= Stirling1($n-1, $k-1)) +
          ($seen{"{$n-1}|{$k}"  } //= Stirling1($n-1, $k  )) * ($n-1)

}

my $upto = 12; my $width = 1 + length max map { Stirling1($upto,$_) } 0..$upto;

say 'Unsigned Stirling1 numbers of the first kind: S1(n, k):'; print 'n\k' . sprintf "%${width}s"x(1+$upto)."\n", 0..$upto;

for my $row (0..$upto) {

   printf '%-3d', $row;
   printf "%${width}d", Stirling1($row, $_) for 0..$row;
   print "\n";

}

say "\nMaximum value from the S1(100, *) row:"; say max map { Stirling1(100,$_) } 0..100;</lang>

Output:
Unsigned Stirling1 numbers of the first kind: S1(n, k):
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         2         3         1
4           0         6        11         6         1
5           0        24        50        35        10         1
6           0       120       274       225        85        15         1
7           0       720      1764      1624       735       175        21         1
8           0      5040     13068     13132      6769      1960       322        28         1
9           0     40320    109584    118124     67284     22449      4536       546        36         1
10          0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11          0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12          0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

Maximum value from the S1(100, *) row:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

Phix

Library: Phix/mpfr
Translation of: Go
with javascript_semantics 
include mpfr.e
 
constant lim = 100,
         lim1 = lim+1,
         last = 12
bool unsigned = true
sequence s1 = repeat(0,lim1)
for n=1 to lim1 do
    s1[n] = mpz_inits(lim1)
end for
mpz_set_si(s1[1][1],1)
mpz {t, m100} = mpz_inits(2)
for n=1 to lim do
    for k=1 to n do
        mpz_set_si(t,n-1)
        mpz_mul(t,t,s1[n][k+1])
        if unsigned then
            mpz_add(s1[n+1][k+1],s1[n][k],t)
        else
            mpz_sub(s1[n+1][k+1],s1[n][k],t)
        end if
    end for
end for
string s = iff(unsigned?"Uns":"S")
printf(1,"%signed Stirling numbers of the first kind: S1(n, k):\n n   k:",s)
for i=0 to last do
    printf(1,"%5d     ", i)
end for
printf(1,"\n---    %s\n",repeat('-',last*10+5))
for n=0 to last do
    printf(1,"%2d ", n)
    for k=1 to n+1 do
        printf(1,"%9s ",{mpz_get_str(s1[n+1][k])})
    end for
    printf(1,"\n")
end for
for k=1 to lim1 do
    mpz s100k = s1[lim1][k] 
    if mpz_cmp(s100k,m100) > 0 then
        mpz_set(m100,s100k)
    end if
end for
printf(1,"\nThe maximum S1(100,k): %s\n",shorten(mpz_get_str(m100)))
Output:
Unsigned Stirling numbers of the first kind: S1(n, k):
 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         2         3         1
 4         0         6        11         6         1
 5         0        24        50        35        10         1
 6         0       120       274       225        85        15         1
 7         0       720      1764      1624       735       175        21         1
 8         0      5040     13068     13132      6769      1960       322        28         1
 9         0     40320    109584    118124     67284     22449      4536       546        36         1
10         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

The maximum S1(100,k): 1971090874705526110...6000000000000000000 (158 digits)

Prolog

Works with: SWI Prolog

<lang prolog>:- dynamic stirling1_cache/3.

stirling1(N, N, 1):-!. stirling1(_, 0, 0):-!. stirling1(N, K, 0):- K > N, !. stirling1(N, K, L):- stirling1_cache(N, K, L), !. stirling1(N, K, L):- N1 is N - 1, K1 is K - 1, stirling1(N1, K, L1), stirling1(N1, K1, L2), !, L is L2 + (N - 1) * L1, assertz(stirling1_cache(N, K, L)).

print_stirling_numbers(N):- between(1, N, K), stirling1(N, K, L), writef('%10r', [L]), fail. print_stirling_numbers(_):- nl.

print_stirling_numbers_up_to(M):- between(1, M, N), print_stirling_numbers(N), fail. print_stirling_numbers_up_to(_).

max_stirling1(N, Max):-

   aggregate_all(max(L), (between(1, N, K), stirling1(N, K, L)), Max).

main:- writeln('Unsigned Stirling numbers of the first kind up to S1(12,12):'), print_stirling_numbers_up_to(12), writeln('Maximum value of S1(n,k) where n = 100:'), max_stirling1(100, M), writeln(M).</lang>

Output:
Unsigned Stirling numbers of the first kind up to S1(12,12):
         1
         1         1
         2         3         1
         6        11         6         1
        24        50        35        10         1
       120       274       225        85        15         1
       720      1764      1624       735       175        21         1
      5040     13068     13132      6769      1960       322        28         1
     40320    109584    118124     67284     22449      4536       546        36         1
    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
Maximum value of S1(n,k) where n = 100:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

PureBasic

Translation of: FreeBasic

<lang PureBasic>EnableExplicit

  1. MAX=12
  2. LZ=10
  3. SP$=" "

Dim s1.i(#MAX,#MAX) Define n.i,k.i,x.i,s$,esc$

s1(0,0)=1 esc$=#ESC$+"[8;24;170t" ; Enlarges the console window

For n=0 To #MAX

 For k=1 To n
   s1(n,k)=s1(n-1,k-1)-(n-1)*s1(n-1,k)
 Next

Next

If OpenConsole()

 Print(esc$)  
 PrintN(~"Signed Stirling numbers of the first kind\n")  
 Print(#SP$+"k")
 For x=0 To #MAX : Print(#SP$+RSet(Str(x),#LZ)) : Next
 PrintN(~"\n  n"+LSet("-",13*12,"-"))
 For n=0 To #MAX
   Print(RSet(Str(n),3))
   For k=0 To #MAX : Print(#SP$+RSet(Str(s1(n,k)),#LZ)) : Next
   PrintN("")
 Next  
 Input()

EndIf</lang>

Output:
Signed Stirling numbers of the first 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           2          -3           1           0           0           0           0           0           0           0           0           0
  4           0          -6          11          -6           1           0           0           0           0           0           0           0           0
  5           0          24         -50          35         -10           1           0           0           0           0           0           0           0
  6           0        -120         274        -225          85         -15           1           0           0           0           0           0           0
  7           0         720       -1764        1624        -735         175         -21           1           0           0           0           0           0
  8           0       -5040       13068      -13132        6769       -1960         322         -28           1           0           0           0           0
  9           0       40320     -109584      118124      -67284       22449       -4536         546         -36           1           0           0           0
 10           0     -362880     1026576    -1172700      723680     -269325       63273       -9450         870         -45           1           0           0
 11           0     3628800   -10628640    12753576    -8409500     3416930     -902055      157773      -18150        1320         -55           1           0
 12           0   -39916800   120543840  -150917976   105258076   -45995730    13339535    -2637558      357423      -32670        1925         -66           1

Python

Translation of: Java

<lang Python> computed = {}

def sterling1(n, k): key = str(n) + "," + str(k)

if key in computed.keys(): return computed[key] if n == k == 0: return 1 if n > 0 and k == 0: return 0 if k > n: return 0 result = sterling1(n - 1, k - 1) + (n - 1) * sterling1(n - 1, k) computed[key] = result return result

print("Unsigned Stirling numbers of the first kind:") MAX = 12 print("n/k".ljust(10), end="") for n in range(MAX + 1): print(str(n).rjust(10), end="") print() for n in range(MAX + 1): print(str(n).ljust(10), end="") for k in range(n + 1): print(str(sterling1(n, k)).rjust(10), end="") print() print("The maximum value of S1(100, k) = ") previous = 0 for k in range(1, 100 + 1): current = sterling1(100, k) if current > previous: previous = current else: print("{0}\n({1} digits, k = {2})\n".format(previous, len(str(previous)), k - 1)) break </lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
4                  0         6        11         6         1
5                  0        24        50        35        10         1
6                  0       120       274       225        85        15         1
7                  0       720      1764      1624       735       175        21         1
8                  0      5040     13068     13132      6769      1960       322        28         1
9                  0     40320    109584    118124     67284     22449      4536       546        36         1
10                 0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11                 0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12                 0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
The maximum value of S1(100, k) =
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)

Quackery

<lang Quackery> [ dip number$

   over size - 
   space swap of
   swap join echo$ ]      is justify  ( n n -->   )
 [ table ]                is s1table  (   n --> n )
 [ swap 101 * + s1table ] is s1       ( n n --> n )
 101 times
   [ i^ 101 times
     [ dup i^ 
         [ over 0 =
           over 0 = and iff 
             [ 2drop 1 ] done
           over 0 >
           over 0 = and iff
             [ 2drop 0 ] done
           2dup < iff
             [ 2drop 0 ] done
           2dup 1 - swap 
           1 - swap s1 unrot
           dip [ 1 - dup ] 
           s1 * + ]
       ' s1table put ]
     drop ]
 say "Unsigned Stirling numbers of the first kind."
 cr cr
 13 times 
   [ i^ dup 1+ times
     [ dup i^ s1
       10 justify ]
     drop cr ]
 cr
 0 100 times
   [ 100 i^ 1+ s1 max ]
 echo cr</lang>
Output:
Unsigned Stirling numbers of the first kind.

         1
         0         1
         0         1         1
         0         2         3         1
         0         6        11         6         1
         0        24        50        35        10         1
         0       120       274       225        85        15         1
         0       720      1764      1624       735       175        21         1
         0      5040     13068     13132      6769      1960       322        28         1
         0     40320    109584    118124     67284     22449      4536       546        36         1
         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

Raku

(formerly Perl 6)

Works with: Rakudo version 2019.07.1

<lang perl6>sub Stirling1 (Int \n, Int \k) {

   return 1 unless n || k;
   return 0 unless n && k;
   state %seen;
   (%seen{"{n - 1}|{k - 1}"} //= Stirling1(n - 1, k - 1)) +
   (n - 1) * (%seen{"{n - 1}|{k}"} //= Stirling1(n - 1, k))

}

my $upto = 12;

my $mx = (1..^$upto).map( { Stirling1($upto, $_) } ).max.chars;

put 'Unsigned Stirling numbers of the first kind: S1(n, k):'; put 'n\k', (0..$upto)».fmt: "%{$mx}d";

for 0..$upto -> $row {

   $row.fmt('%-3d').print;
   put (0..$row).map( { Stirling1($row, $_) } )».fmt: "%{$mx}d";

}

say "\nMaximum value from the S1(100, *) row:"; say (^100).map( { Stirling1 100, $_ } ).max;</lang>

Output:
Unsigned Stirling numbers of the first kind: S1(n, k):
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         2         3         1
4          0         6        11         6         1
5          0        24        50        35        10         1
6          0       120       274       225        85        15         1
7          0       720      1764      1624       735       175        21         1
8          0      5040     13068     13132      6769      1960       322        28         1
9          0     40320    109584    118124     67284     22449      4536       546        36         1
10         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1

Maximum value from the S1(100, *) row:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

REXX

Some extra code was added to minimize the displaying of the column widths. <lang rexx>/*REXX program to compute and display (unsigned) Stirling numbers of the first kind.*/ parse arg lim . /*obtain optional argument from the CL.*/ if lim== | lim=="," then lim= 12 /*Not specified? Then use the default.*/ olim= lim /*save the original value of LIM. */ lim= abs(lim) /*only use the absolute value of LIM. */ numeric digits max(9, 2*lim) /*(over) specify maximum number in grid*/ @.=; @.0.0= 1 /*define the (0, 0)th value in grid*/

       do n=0  for lim+1                        /* [↓]  initialize some  values  "   " */
       if n>0  then @.n.0 = 0                   /*assign value if  N > zero.           */
         do k=n+1  to lim
         @.n.k = 0                              /*zero some values in grid  if  K > N. */
         end   /*k*/
       end     /*n*/

max#.= 0 /* [↓] calculate values for the grid. */

       do   n=1  for lim;           nm= n - 1
         do k=1  for lim;           km= k - 1
         @.n.k = @.nm.km  +  nm * @.nm.k        /*calculate an unsigned number in grid.*/
         max#.k= max(max#.k, @.n.k)             /*find the      maximum value   "   "  */
         max#.b= max(max#.b, @.n.k)             /*find the maximum value for all rows. */
         end   /*k*/
       end     /*n*/
       do k=1  for lim                          /*find max column width for each column*/
       max#.a= max#.a + length(max#.k)
       end   /*k*/
                                                /* [↓]  only show the maximum value ?  */

w= length(max#.b) /*calculate max width of all numbers. */ if olim<0 then do; say 'The maximum value (which has ' w " decimal digits):"

                    say max#.b                  /*display maximum number in the grid.  */
                    exit                        /*stick a fork in it,  we're all done. */
               end

wi= max(3, length(lim+1) ) /*the maximum width of the grid's index*/ say '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 right(r,wi)  strip(substr($,2), 'T') /*display a single row of the grid.    */
       end     /*r*/                            /*stick a fork in it,  we're all done. */</lang>
output   when using the default input:
row ════════════════════════════════════════columns═════════════════════════════════════════
  0 1
  1 0        1
  2 0        1         1
  3 0        2         3         1
  4 0        6        11         6         1
  5 0       24        50        35        10        1
  6 0      120       274       225        85       15        1
  7 0      720      1764      1624       735      175       21       1
  8 0     5040     13068     13132      6769     1960      322      28      1
  9 0    40320    109584    118124     67284    22449     4536     546     36     1
 10 0   362880   1026576   1172700    723680   269325    63273    9450    870    45    1
 11 0  3628800  10628640  12753576   8409500  3416930   902055  157773  18150  1320   55  1
 12 0 39916800 120543840 150917976 105258076 45995730 13339535 2637558 357423 32670 1925 66 1
output   when using the input of:     -100

(Shown at three-quarter size.)

The maximum value  (which has  158  decimal digits):
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

Ruby

Translation of: D

<lang ruby>$cache = {} def sterling1(n, k)

   if n == 0 and k == 0 then
       return 1
   end
   if n > 0 and k == 0 then
       return 0
   end
   if k > n then
       return 0
   end
   key = [n, k]
   if $cache[key] then
       return $cache[key]
   end
   value = sterling1(n - 1, k - 1) + (n - 1) * sterling1(n - 1, k)
   $cache[key] = value
   return value

end

MAX = 12 def main

   print "Unsigned Stirling numbers of the first kind:\n"
   print "n/k"
   for n in 0 .. MAX
       print "%10d" % [n]
   end
   print "\n"
   for n in 0 .. MAX
       print "%-3d" % [n]
       for k in 0 .. n
           print "%10d" % [sterling1(n, k)]
       end
       print "\n"
   end
   print "The maximum value of S1(100, k) =\n"
   previous = 0
   for k in 1 .. 100
       current = sterling1(100, k)
       if previous < current then
           previous = current
       else
           print previous, "\n"
           print "(%d digits, k = %d)\n" % [previous.to_s.length, k - 1]
           break
       end
   end

end

main()</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
4           0         6        11         6         1
5           0        24        50        35        10         1
6           0       120       274       225        85        15         1
7           0       720      1764      1624       735       175        21         1
8           0      5040     13068     13132      6769      1960       322        28         1
9           0     40320    109584    118124     67284     22449      4536       546        36         1
10          0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11          0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12          0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
The maximum value of S1(100, k) =
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)

Sidef

<lang ruby>func S1(n, k) { # unsigned Stirling numbers of the first kind

   stirling(n, k).abs

}

const r = (0..12)

var triangle = r.map {|n| 0..n -> map {|k| S1(n, k) } } var widths = r.map {|n| r.map {|k| (triangle[k][n] \\ 0).len }.max }

say ('n\k ', r.map {|n| "%*s" % (widths[n], n) }.join(' '))

r.each {|n|

   var str = ('%-3s ' % n)
   str += triangle[n].map_kv {|k,v| "%*s" % (widths[k], v) }.join(' ')
   say str

}

with (100) {|n|

   say "\nMaximum value from the S1(#{n}, *) row:"
   say { S1(n, _) }.map(^n).max

}</lang>

Output:
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        2         3         1
4   0        6        11         6         1
5   0       24        50        35        10        1
6   0      120       274       225        85       15        1
7   0      720      1764      1624       735      175       21       1
8   0     5040     13068     13132      6769     1960      322      28      1
9   0    40320    109584    118124     67284    22449     4536     546     36     1
10  0   362880   1026576   1172700    723680   269325    63273    9450    870    45    1
11  0  3628800  10628640  12753576   8409500  3416930   902055  157773  18150  1320   55  1
12  0 39916800 120543840 150917976 105258076 45995730 13339535 2637558 357423 32670 1925 66 1

Maximum value from the S1(100, *) row:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000

Alternatively, the S1(n,k) function can be defined as: <lang ruby>func S1((0), (0)) { 1 } func S1(_, (0)) { 0 } func S1((0), _) { 0 } func S1(n, k) is cached { S1(n-1, k-1) + (n-1)*S1(n-1, k) }</lang>

Tcl

This computes the unsigned Stirling numbers of the first kind. Inspired by Stirling_numbers_of_the_second_kind#Tcl, hence similar to #Java. <lang Tcl>proc US1 {n k} {

   if {$k == 0} {
       return [expr {$n == 0}]
   }
   if {$n < $k} {
       return 0
   }
   if {$n == $k} {
       return 1
   }
   set nk [list $n $k]
   if {[info exists ::US1cache($nk)]} {
       return      $::US1cache($nk)
   }
   set n1 [expr {$n - 1}]
   set k1 [expr {$k - 1}]
   set r  [expr {($n1 * [US1 $n1 $k]) + [US1 $n1 $k1]}]
   set ::US1cache($nk) $r

}

proc main {} {

   puts "Unsigned Stirling numbers of the first kind:"
   set max 12                  ;# last table line to print
   set L   9                   ;# space to use for 1 number
   puts -nonewline "n\\k"
   for {set n 0} {$n <= $max} {incr n} {
       puts -nonewline " [format %${L}d $n]"
   }
   puts ""
   for {set n 0} {$n <= $max} {incr n} {
       puts -nonewline [format %-3d $n]
       for {set k 0} {$k <= $n} {incr k} {
           puts -nonewline " [format %${L}s [US1 $n $k]]"
       }
       puts ""
   }
   puts "The maximum value of US1(100, k) = "
   set previous 0
   for {set k 1} {$k <= 100} {incr k} {
       set current [US1 100 $k]
       if {$current > $previous} {
           set previous $current
       } else {
           puts $previous
           puts "([string length $previous] digits, k = [expr {$k-1}])"
           break
       }
   }

} main</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
4           0         6        11         6         1
5           0        24        50        35        10         1
6           0       120       274       225        85        15         1
7           0       720      1764      1624       735       175        21         1
8           0      5040     13068     13132      6769      1960       322        28         1
9           0     40320    109584    118124     67284     22449      4536       546        36         1
10          0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11          0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12          0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
The maximum value of US1(100, k) = 
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)

Wren

Translation of: Java
Library: Wren-big
Library: Wren-fmt

<lang ecmascript>import "/big" for BigInt import "/fmt" for Fmt

var computed = {}

var stirling1 // recursive stirling1 = 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) return BigInt.zero
   if (k > n) return BigInt.zero
   var result = stirling1.call(n-1, k-1) + stirling1.call(n-1, k)*(n-1)
   computed[key] = result
   return result

}

System.print("Unsigned Stirling numbers of the first 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", stirling1.call(n, k))
   System.print()

} System.print("The maximum value of S1(100, k) =") var previous = BigInt.zero for (k in 1..100) {

   var current = stirling1.call(100, k)
   if (current > previous) {
       previous = current
   } else {
       Fmt.print("$i\n($d digits, k = $d)", previous, previous.toString.count, k - 1)
       break
   }

}</lang>

Output:
Unsigned Stirling numbers of the first 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         2         3         1
4           0         6        11         6         1
5           0        24        50        35        10         1
6           0       120       274       225        85        15         1
7           0       720      1764      1624       735       175        21         1
8           0      5040     13068     13132      6769      1960       322        28         1
9           0     40320    109584    118124     67284     22449      4536       546        36         1
10          0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
11          0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
12          0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
The maximum value of S1(100, k) =
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)

zkl

Translation of: Raku

<lang zkl>fcn stirling1(n,k){

  var seen=Dictionary();	// cache for recursion
  if(n==k==0)      return(1);
  if(n==0 or k==0) return(0);
  
  z1,z2 := "%d,%d".fmt(n-1,k-1), "%d,%d".fmt(n-1,k);
  if(Void==(s1 := seen.find(z1))){ s1 = seen[z1] = stirling1(n - 1, k - 1) }
  if(Void==(s2 := seen.find(z2))){ s2 = seen[z2] = stirling1(n - 1, k)     }
  (n - 1)*s2 + s1;   // n is first to cast to BigInt (if using BigInts)

}</lang> <lang zkl>// calculate entire table (cached), find max, find num digits in max N,mx := 12, [1..N].apply(fcn(n){ [1..n].apply(stirling1.fp(n)) : (0).max(_) }) : (0).max(_); fmt:="%%%dd".fmt("%d".fmt(mx.numDigits + 1)).fmt; // "%9d".fmt println("Unsigned Stirling numbers of the first kind: S1(n, k):"); println("n\\k",[0..N].pump(String,fmt)); foreach row in ([0..N]){

  println("%3d".fmt(row), [0..row].pump(String, stirling1.fp(row), fmt));

}</lang>

Output:
Unsigned Stirling numbers of the first kind: S1(n, k):
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         2         3         1
  4         0         6        11         6         1
  5         0        24        50        35        10         1
  6         0       120       274       225        85        15         1
  7         0       720      1764      1624       735       175        21         1
  8         0      5040     13068     13132      6769      1960       322        28         1
  9         0     40320    109584    118124     67284     22449      4536       546        36         1
 10         0    362880   1026576   1172700    723680    269325     63273      9450       870        45         1
 11         0   3628800  10628640  12753576   8409500   3416930    902055    157773     18150      1320        55         1
 12         0  39916800 120543840 150917976 105258076  45995730  13339535   2637558    357423     32670      1925        66         1
Library: GMP

GNU Multiple Precision Arithmetic Library

<lang zkl>var [const] BI=Import("zklBigNum"); // libGMP N=100; println("Maximum value from the S1(%d, *) row:".fmt(N)); [1..N].apply(stirling1.fp(BI(N)))

 .reduce(fcn(m,n){ m.max(n) }).println();</lang>
Output:
Maximum value from the S1(100, *) row:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000