Stirling numbers of the first kind: Difference between revisions

m (Updated description and link for Fōrmulæ solution)
 
(14 intermediate revisions by 8 users not shown)
Line 52:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">[(Int, Int) = BigInt] computed
 
F sterling1(n, k)
Line 88:
E
print("#.\n(#. digits, k = #.)\n".format(previous, String(previous).len, k - 1))
L.break</langsyntaxhighlight>
 
{{out}}
Line 115:
{{works with|ALGOL 68G|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.
<langsyntaxhighlight lang="algol68">BEGIN
BEGIN # show some (unsigned) Stirling numbers of the first kind #
 
# specify the precision of LONG LONG INT, we need about 160 digits #
Line 145:
REF[,]SINT s1 = make s1( max stirling, FALSE );
print( ( "Unsigned Stirling numbers of the first kind:", newline ) );
print( ( " k 0" ) );
FOR k FROM 0 TO max stirling DO print( ( whole( k, IF k < 6 THEN -10 ELSE -9 FI ) ) ) OD;
print( ( newline, " n", newline ) );
FOR n FROM 0 TO max stirling DO
print( ( whole( n, -2 ), whole( s1[ n, 0 ], -3 ) ) );
FOR k FROM 0 TO n DO
print( ( whole( s1[ n, k ], IF k < 6 THEN -10 ELSE -9 FI ) ) )
OD;
print( ( newline ) )
Line 167:
print( ( whole( max 100, 0 ), newline ) )
END
END</lang>
</syntaxhighlight>
{{out}}
<pre>
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
Line 191 ⟶ 192:
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
<lang algolw>begin % show some (unsigned) Stirling numbers of the first kind %
begin % show some (unsigned) Stirling numbers of the first kind %
integer MAX_STIRLING;
MAX_STIRLING := 12;
Line 211 ⟶ 213:
% print the Stirling numbers up to n, k = 12 %
write( "Unsigned Stirling numbers of the first kind:" );
write( " k 0" );
for k := 01 until MAX_STIRLING do writeon( i_w := if k < 6 then 10 else 9, s_w := 0, k );
write( " n" );
for n := 0 until MAX_STIRLING do begin
write( i_w := 2, s_w := 0, n, i_w := 3, s1( n, 0 ) );
for k := 01 until n do begin
writeon( i_w := if k < 6 then 10 else 9, s_w := 0, s1( n, k ) )
end for_k
end for_n
end
end.</lang>
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 304 ⟶ 307:
stirling_cache_destroy(&sc);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 327 ⟶ 330:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iostream>
Line 379 ⟶ 382:
std::cout << max << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 404 ⟶ 407:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.bigint;
import std.functional;
import std.stdio;
Line 451 ⟶ 454:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling numbers of the first kind:
Line 471 ⟶ 474:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
print "Unsigned Stirling numbers of the first 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
b[k] = a[k - 1] + (n - 1) * a[k]
write b[k] & " "
.
print ""
swap a[] b[]
.
</syntaxhighlight>
 
=={{header|Factor}}==
Line 477 ⟶ 499:
For example, <tt>x(x-1)(x-2) = x<sup>3</sup> - 3x<sup>2</sup> + 2x</tt>. Taking the absolute values of the coefficients, the third row is <tt>(0) 2 3 1</tt>.
{{works with|Factor|0.99 development version 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: arrays assocs formatting io kernel math math.polynomials
math.ranges prettyprint sequences ;
IN: rosetta-code.stirling-first
Line 494 ⟶ 516:
 
"Maximum value from 100th stirling row:" print
100 stirling-row supremum .</langsyntaxhighlight>
{{out}}
<pre>
Line 518 ⟶ 540:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight 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
Line 548 ⟶ 570:
next k
print outstr
next n</langsyntaxhighlight>
<pre>Signed Stirling numbers of the first kind
 
Line 569 ⟶ 591:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Stirling_numbers_of_the_first_kind}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. 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 storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
A single solution for signed and unsigned versions is presented. Unsigned version uses 1 as factor, signed version used -1.
 
'''Version 1. Recursive'''
 
[[File:Fōrmulæ - Stirling numbers of the first kind 01.png]]
 
'''Test case 1. Show the unsigned Stirling numbers of the first kind, S₁(n, k), up to S₁(12, 12)'''
 
[[File:Fōrmulæ - Stirling numbers of the first kind 02.png]]
 
[[File:Fōrmulæ - Stirling numbers of the first kind 03.png]]
 
'''Test case 2. Show the signed Stirling numbers of the first kind, S₁(n, k), up to S₁(12, 12)'''
 
[[File:Fōrmulæ - Stirling numbers of the first kind 04.png]]
 
[[File:Fōrmulæ - Stirling numbers of the first kind 05.png]]
 
'''Version 2. Non recursive'''
 
A faster, non recursive version is presented. This construct a matrix.
 
[[File:Fōrmulæ - Stirling numbers of the first kind 06.png]]
 
'''Test case 1. Show the unsigned Stirling numbers of the first kind, S₁(n, k), up to S₁(12, 12)'''
 
[[File:Fōrmulæ - Stirling numbers of the first kind 07.png]]
 
(the result is the same as recursive version)
 
'''Test case 2. Show the signed Stirling numbers of the first kind, S₁(n, k), up to S₁(12, 12)'''
 
[[File:Fōrmulæ - Stirling numbers of the first kind 08.png]]
 
(the result is the same as recursive version)
 
'''Test case 3. Find the maximum value of unsigned S₁(n, k) where n ≤ 100'''
 
[[File:Fōrmulæ - Stirling numbers of the first kind 09.png]]
 
[[File:Fōrmulæ - Stirling numbers of the first kind 10.png]]
 
(the result is the same as recursive version)
 
'''Test case 4. Find the maximum value of signed S₁(n, k) where n ≤ 100'''
 
[[File:Fōrmulæ - Stirling numbers of the first kind 11.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Stirling numbers of the first kind 12.png]]
In '''[https://formulae.org/?example=Stirling_numbers_of_the_first_kind this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 633 ⟶ 703:
fmt.Println(max)
fmt.Printf("which has %d digits.\n", len(max.String()))
}</langsyntaxhighlight>
 
{{out}}
Line 660 ⟶ 730:
=={{header|Haskell}}==
Using library: data-memocombinators for memoization
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
import Data.List (groupBy)
import qualified Data.MemoCombinators as Memo
Line 684 ⟶ 754:
where
table :: [[(Int, Int)]]
table = groupBy (\a b -> fst a == fst b) $ (,) <$> [0..12] <*> [0..12]</langsyntaxhighlight>
 
Or library: monad-memo for memoization.
<langsyntaxhighlight lang="haskell">{-# LANGUAGE FlexibleContexts #-}
 
import Text.Printf (printf)
Line 715 ⟶ 785:
where
table :: [[(Int, Int)]]
table = groupBy (\a b -> fst a == fst b) $ (,) <$> [0..12] <*> [0..12]</langsyntaxhighlight>
{{out}}
<pre>n/k 0 1 2 3 4 5 6 7 8 9 10 11 12
Line 778 ⟶ 848:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.HashMap;
Line 836 ⟶ 906:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 859 ⟶ 929:
(158 digits, k = 5)
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The Go implementation of jq supports unbounded-precision integer arithmetic and so the results shown below for max(S(100,k)) are based on a run of gojq.
<syntaxhighlight lang="jq"># For efficiency, define the helper function for `stirling1/2` as a
# top-level function so we can use it directly for constructing the table:
# input: [m,j,cache]
# output: [s, cache]
def stirling1:
. as [$m, $j, $cache]
| "\($m),\($j)" as $key
| $cache
| if has($key) then [.[$key], .]
elif $m == 0 and $j == 0 then [1, .]
elif $m > 0 and $j == 0 then [0, .]
elif $j > $m then [0, .]
else ([$m-1, $j-1, .] | stirling1) as [$s1, $c1]
| ([$m-1, $j, $c1] | stirling1) as [$s2, $c2]
| (($s1 + ($m-1) * $s2)) as $result
| ($c2 | (.[$key] = $result)) as $c3
| [$result, $c3]
end;
 
def stirling1($n; $k):
[$n, $k, {}] | stirling1 | .[0];
 
# produce the table for 0 ... $n inclusive
def stirlings($n):
# state: [cache, array_of_results]
reduce range(0; $n+1) as $i ([{}, []];
reduce range(0; $i+1) as $j (.;
. as [$cache, $a]
| ([$i, $j, $cache] | stirling1) as [$s, $c]
| [$c, ($a|setpath([$i,$j]; $s))] ));
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def task($n):
"Unsigned Stirling numbers of the first kind:",
"n/k \( [range(0;$n+1)|lpad(10)] | join(" "))",
((stirlings($n) | .[1]) as $a
| range(0; $n+1) as $i
| "\($i|lpad(3)): \( [$a[$i][]| lpad(10)] | join(" ") )" ),
"\nThe maximum value of S1(100, k) is",
([stirling1(100; range(0;101)) ] | max) ;
 
task(12)</syntaxhighlight>
{{out}}
<pre>
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) is
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
const s1cache = Dict()
Line 905 ⟶ 1,047:
printstirling1table(12)
println("\nThe maximum for stirling1(100, _) is:\n", maximum(k-> stirlings1(BigInt(100), BigInt(k)), 1:100))
</langsyntaxhighlight>{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 10 11 12
Line 928 ⟶ 1,070:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
 
fun main() {
Line 978 ⟶ 1,120:
COMPUTED[key] = result
return result
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling numbers of the first kind:
Line 998 ⟶ 1,140:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- show some (unsigned) Stirling numbers of the first kind
local MAX_STIRLING = 12
-- construct a matrix of Stirling numbers up to max n, max n
local s1 = {}
for n = 0, MAX_STIRLING do
s1[ n ] = {}
for k = 0, MAX_STIRLING do s1[ n ][ k ] = 0 end
end
s1[ 0 ][ 0 ] = 1
for n = 1, MAX_STIRLING do s1[ n ][ 0 ] = 0 end
for n = 1, MAX_STIRLING do
for k = 1, n do
local s1Term = ( ( n - 1 ) * s1[ n - 1 ][ k ] )
s1[ n ][ k ] = s1[ n - 1 ][ k - 1 ] + s1Term
end
end
io.write( "Unsigned Stirling numbers of the first kind:\n" )
io.write( " k 0" )
for k = 1, MAX_STIRLING do
io.write( string.format( ( k < 6 and "%10d" or "%9d" ), k ) )
end
io.write( "\n" )
io.write( " n\n" );
for n = 0, MAX_STIRLING do
io.write( string.format( "%2d", n ), string.format( "%3d", s1[ n ][ 0 ] ) )
for k = 1, n do
io.write( string.format( ( k < 6 and "%10d" or "%9d" ), s1[ n ][ k ] ) )
end
io.write( "\n" )
end
end
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="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]]</langsyntaxhighlight>
{{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
Line 1,024 ⟶ 1,220:
A simple implementation using the recursive definition is enough to complete the task.
 
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils
 
proc s1(n, k: Natural): Natural =
Line 1,037 ⟶ 1,233:
for k in 0..n:
stdout.write ($s1(n, k)).align(10)
stdout.write '\n'</langsyntaxhighlight>
 
{{out}}
Line 1,060 ⟶ 1,256:
{{libheader|bignum}}
 
<langsyntaxhighlight Nimlang="nim">import tables
import bignum
 
Line 1,079 ⟶ 1,275:
 
echo "Maximum Stirling number of the first kind with n = 100:"
echo max</langsyntaxhighlight>
 
{{out}}
Line 1,087 ⟶ 1,283:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use bigint;
Line 1,117 ⟶ 1,313:
 
say "\nMaximum value from the S1(100, *) row:";
say max map { Stirling1(100,$_) } 0..100;</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling1 numbers of the first kind: S1(n, k):
Line 1,141 ⟶ 1,337:
{{libheader|Phix/mpfr}}
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 1,186 ⟶ 1,382:
<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 S1(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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,211 ⟶ 1,407:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">:- dynamic stirling1_cache/3.
 
stirling1(N, N, 1):-!.
Line 1,252 ⟶ 1,448:
writeln('Maximum value of S1(n,k) where n = 100:'),
max_stirling1(100, M),
writeln(M).</langsyntaxhighlight>
 
{{out}}
Line 1,275 ⟶ 1,471:
=={{header|PureBasic}}==
{{trans|FreeBasic}}
<langsyntaxhighlight PureBasiclang="purebasic">EnableExplicit
#MAX=12
#LZ=10
Line 1,303 ⟶ 1,499:
Next
Input()
EndIf</langsyntaxhighlight>
{{out}}
<pre>Signed Stirling numbers of the first kind
Line 1,326 ⟶ 1,522:
=={{header|Python}}==
{{trans|Java}}
<syntaxhighlight lang="python">
<lang Python>
computed = {}
 
Line 1,364 ⟶ 1,560:
print("{0}\n({1} digits, k = {2})\n".format(previous, len(str(previous)), k - 1))
break
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,389 ⟶ 1,585:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ dip number$
over size -
space swap of
Line 1,426 ⟶ 1,622:
0 100 times
[ 100 i^ 1+ s1 max ]
echo cr</langsyntaxhighlight>
 
{{out}}
Line 1,453 ⟶ 1,649:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>sub Stirling1 (Int \n, Int \k) {
return 1 unless n || k;
return 0 unless n && k;
Line 1,474 ⟶ 1,670:
 
say "\nMaximum value from the S1(100, *) row:";
say (^100).map( { Stirling1 100, $_ } ).max;</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling numbers of the first kind: S1(n, k):
Line 1,497 ⟶ 1,693:
=={{header|REXX}}==
Some extra code was added to minimize the displaying of the column widths.
<langsyntaxhighlight 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.*/
Line 1,536 ⟶ 1,732:
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. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,561 ⟶ 1,757:
The maximum value (which has 158 decimal digits):
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
</pre>
 
=={{header|RPL}}==
« '''IF''' DUP2 AND NOT '''THEN''' ==
'''ELSE'''
SWAP 1 - DUP ROT
DUP2 1 - <span style="color:blue">US1</span> 4 ROLLD <span style="color:blue">US1</span> * +
'''END'''
» '<span style="color:blue">US1</span>' STO <span style="color:grey">''@ ( n k → unsigned S1(n,k) )''</span>
« { 12 12 } 0 CON
1 12 '''FOR''' n
1 n '''FOR''' k
n k 2 →LIST DUP EVAL <span style="color:blue">US1</span> PUT
'''NEXT NEXT'''
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: [[ 1 0 0 0 0 0 0 0 0 0 0 0 ]
[ 1 1 0 0 0 0 0 0 0 0 0 0 ]
[ 2 3 1 0 0 0 0 0 0 0 0 0 ]
[ 6 11 6 1 0 0 0 0 0 0 0 0 ]
[ 24 50 35 10 1 0 0 0 0 0 0 0 ]
[ 120 274 225 85 15 1 0 0 0 0 0 0 ]
[ 720 1764 1624 735 175 21 1 0 0 0 0 0 ]
[ 5040 13068 13132 6769 1960 322 28 1 0 0 0 0 ]
[ 40320 109584 118124 67284 22449 4536 546 36 1 0 0 0 ]
[ 362880 1026576 1172700 723680 269325 63273 9450 870 45 1 0 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 ]]
</pre>
 
=={{header|Ruby}}==
{{trans|D}}
<langsyntaxhighlight lang="ruby">$cache = {}
def sterling1(n, k)
if n == 0 and k == 0 then
Line 1,616 ⟶ 1,842:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling numbers of the first kind:
Line 1,638 ⟶ 1,864:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func S1(n, k) { # unsigned Stirling numbers of the first kind
stirling(n, k).abs
}
Line 1,658 ⟶ 1,884:
say "\nMaximum value from the S1(#{n}, *) row:"
say { S1(n, _) }.map(^n).max
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,681 ⟶ 1,907:
 
Alternatively, the '''S1(n,k)''' function can be defined as:
<langsyntaxhighlight 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) }</langsyntaxhighlight>
 
=={{header|Tcl}}==
This computes the unsigned Stirling numbers of the first kind.
Inspired by [[Stirling_numbers_of_the_second_kind#Tcl]], hence similar to [[#Java]].
<langsyntaxhighlight Tcllang="tcl">proc US1 {n k} {
if {$k == 0} {
return [expr {$n == 0}]
Line 1,740 ⟶ 1,966:
}
}
main</langsyntaxhighlight>
 
{{out}}
Line 1,765 ⟶ 1,991:
{{trans|Java}}{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
import "./fmt" for Fmt
 
var computed = {}
Line 1,802 ⟶ 2,028:
break
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,824 ⟶ 2,050:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)
</pre>
 
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0"> define MAX_STIRLING = 12;
integer S1 ( MAX_STIRLING+1, MAX_STIRLING+1 );
integer N, K, S1Term;
begin
\Construct a matrix of Stirling numbers up to max N, max N
for N := 0 to MAX_STIRLING do begin
for K := 0 to MAX_STIRLING do S1( N, K ) := 0
end; \for_N
S1( 0, 0 ) := 1;
for N := 1 to MAX_STIRLING do S1( N, 0 ) := 0;
for N := 1 to MAX_STIRLING do begin
for K := 1 to N do begin
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
Text(0, "Unsigned Stirling numbers of the first kind:^m^j K" );
Format(10, 0);
for K := 0 to MAX_STIRLING do RlOut(0, float(K) );
CrLf(0);
Text(0, " N^m^j" );
for N := 0 to MAX_STIRLING do begin
Format(2, 0); RlOut(0, float(N));
Format(10, 0);
for K := 0 to N do begin
RlOut(0, float(S1( N, K )) )
end; \for_K
CrLf(0);
end \for_N
end</syntaxhighlight>
{{out}}
<pre style="font-size:66%">
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
</pre>
 
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">fcn stirling1(n,k){
var seen=Dictionary(); // cache for recursion
if(n==k==0) return(1);
Line 1,837 ⟶ 2,116:
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)
}</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(stirling1.fp(n)) : (0).max(_) }) : (0).max(_);
fmt:="%%%dd".fmt("%d".fmt(mx.numDigits + 1)).fmt; // "%9d".fmt
Line 1,845 ⟶ 2,124:
foreach row in ([0..N]){
println("%3d".fmt(row), [0..row].pump(String, stirling1.fp(row), fmt));
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 1,865 ⟶ 2,144:
</pre>
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<langsyntaxhighlight 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();</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
1,150

edits