Stirling numbers of the first kind: Difference between revisions

 
(23 intermediate revisions by 12 users not shown)
Line 48:
 
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight 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</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) =
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)
</pre>
 
=={{header|ALGOL 68}}==
{{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 82 ⟶ 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 104 ⟶ 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
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight 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 0" );
for k := 1 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 := 1 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.
</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 189 ⟶ 307:
stirling_cache_destroy(&sc);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 212 ⟶ 330:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iostream>
Line 264 ⟶ 382:
std::cout << max << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 289 ⟶ 407:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.bigint;
import std.functional;
import std.stdio;
Line 336 ⟶ 454:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling numbers of the first kind:
Line 356 ⟶ 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 362 ⟶ 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 379 ⟶ 516:
 
"Maximum value from 100th stirling row:" print
100 stirling-row supremum .</langsyntaxhighlight>
{{out}}
<pre>
Line 403 ⟶ 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 433 ⟶ 570:
next k
print outstr
next n</langsyntaxhighlight>
<pre>Signed Stirling numbers of the first kind
 
Line 454 ⟶ 591:
=={{header|Fōrmulæ}}==
 
In [{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/Stirling_numbers_of_the_first_kind this] page you can see the solution of this task.}}
 
'''Solution'''
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
A single solution for signed and unsigned versions is presented. Unsigned version uses 1 as factor, signed version used -1.
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
'''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]]
 
[[File:Fōrmulæ - Stirling numbers of the first kind 12.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 518 ⟶ 703:
fmt.Println(max)
fmt.Printf("which has %d digits.\n", len(max.String()))
}</langsyntaxhighlight>
 
{{out}}
Line 545 ⟶ 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 569 ⟶ 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 600 ⟶ 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 663 ⟶ 848:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.HashMap;
Line 721 ⟶ 906:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 744 ⟶ 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 790 ⟶ 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 813 ⟶ 1,070:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
 
fun main() {
Line 863 ⟶ 1,120:
COMPUTED[key] = result
return result
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling numbers of the first kind:
Line 883 ⟶ 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}}==
<syntaxhighlight 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]]</syntaxhighlight>
{{out}}
<pre> k=0 k=1 k=2 k=3 k=4 k=5 k=6 k=7 k=8 k=9 k=10 k=11 k=12
n=0 1 0 0 0 0 0 0 0 0 0 0 0 0
n=1 0 1 0 0 0 0 0 0 0 0 0 0 0
n=2 0 -1 1 0 0 0 0 0 0 0 0 0 0
n=3 0 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</pre>
 
 
 
=={{header|Nim}}==
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 900 ⟶ 1,233:
for k in 0..n:
stdout.write ($s1(n, k)).align(10)
stdout.write '\n'</langsyntaxhighlight>
 
{{out}}
Line 923 ⟶ 1,256:
{{libheader|bignum}}
 
<langsyntaxhighlight Nimlang="nim">import tables
import bignum
 
Line 942 ⟶ 1,275:
 
echo "Maximum Stirling number of the first kind with n = 100:"
echo max</langsyntaxhighlight>
 
{{out}}
Line 950 ⟶ 1,283:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use bigint;
Line 980 ⟶ 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,004 ⟶ 1,337:
{{libheader|Phix/mpfr}}
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include mpfr.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
constant lim = 100,
lim1 = lim+1,
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span><span style="color: #0000FF;">,</span>
last = 12
<span style="color: #000000;">lim1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
bool unsigned = true
<span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">12</span>
sequence s1 = repeat(0,lim1)
<span style="color: #004080;">bool</span> <span style="color: #000000;">unsigned</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
for n=1 to lim1 do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lim1</span><span style="color: #0000FF;">)</span>
s1[n] = mpz_inits(lim1)
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lim1</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">s1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lim1</span><span style="color: #0000FF;">)</span>
mpz_set_si(s1[1][1],1)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
mpz {t, m100} = mpz_inits(2)
<span style="color: #7060A8;">mpz_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
for n=1 to lim do
<span style="color: #004080;">mpz</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m100</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
for k=1 to n do
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lim</span> <span style="color: #008080;">do</span>
mpz_set_si(t,n-1)
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
mpz_mul(t,t,s1[n][k+1])
<span style="color: #7060A8;">mpz_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
if unsigned then
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
mpz_add(s1[n+1][k+1],s1[n][k],t)
<span style="color: #008080;">if</span> <span style="color: #000000;">unsigned</span> <span style="color: #008080;">then</span>
else
<span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">],</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
mpz_sub(s1[n+1][k+1],s1[n][k],t)
<span style="color: #008080;">else</span>
end if
<span style="color: #7060A8;">mpz_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">],</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
string s = iff(unsigned?"Uns":"S")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"%signed Stirling numbers of the first kind: S1(n, k):\n n k:",s)
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unsigned</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"Uns"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"S"</span><span style="color: #0000FF;">)</span>
for i=0 to last do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%signed Stirling numbers of the first kind: S1(n, k):\n n k:"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
printf(1,"%5d ", i)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">last</span> <span style="color: #008080;">do</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%5d "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
printf(1,"\n--- %s\n",repeat('-',last*10+5))
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for n=0 to last do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n--- %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'-'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">last</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span>
printf(1,"%2d ", n)
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">last</span> <span style="color: #008080;">do</span>
for k=1 to n+1 do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
mpfr_printf(1,"%9Zd ", s1[n+1][k])
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%9s "</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">])})</span>
printf(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
for k=1 to lim1 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
mpz s100k = s1[lim1][k]
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lim1</span> <span style="color: #008080;">do</span>
if mpz_cmp(s100k,m100) > 0 then
<span style="color: #004080;">mpz</span> <span style="color: #000000;">s100k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">lim1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
mpz_set(m100,s100k)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s100k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m100</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s100k</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"\nThe maximum S1(100,k): %s\n",shorten(mpz_get_str(m100)))</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nThe maximum 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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,071 ⟶ 1,407:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">:- dynamic stirling1_cache/3.
 
stirling1(N, N, 1):-!.
Line 1,112 ⟶ 1,448:
writeln('Maximum value of S1(n,k) where n = 100:'),
max_stirling1(100, M),
writeln(M).</langsyntaxhighlight>
 
{{out}}
Line 1,135 ⟶ 1,471:
=={{header|PureBasic}}==
{{trans|FreeBasic}}
<langsyntaxhighlight PureBasiclang="purebasic">EnableExplicit
#MAX=12
#LZ=10
Line 1,163 ⟶ 1,499:
Next
Input()
EndIf</langsyntaxhighlight>
{{out}}
<pre>Signed Stirling numbers of the first kind
Line 1,186 ⟶ 1,522:
=={{header|Python}}==
{{trans|Java}}
<syntaxhighlight lang="python">
<lang Python>
computed = {}
 
Line 1,224 ⟶ 1,560:
print("{0}\n({1} digits, k = {2})\n".format(previous, len(str(previous)), k - 1))
break
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,245 ⟶ 1,581:
19710908747055261109287881673376044669240511161402863823515728791076863288440277983854056472903481625299174865860036734731122707870406148096000000000000000000
(158 digits, k = 5)
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight 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</syntaxhighlight>
 
{{out}}
 
<pre>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
</pre>
 
Line 1,251 ⟶ 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,272 ⟶ 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,295 ⟶ 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,334 ⟶ 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,359 ⟶ 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,414 ⟶ 1,842:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>Unsigned Stirling numbers of the first kind:
Line 1,436 ⟶ 1,864:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func S1(n, k) { # unsigned Stirling numbers of the first kind
stirling(n, k).abs
}
Line 1,456 ⟶ 1,884:
say "\nMaximum value from the S1(#{n}, *) row:"
say { S1(n, _) }.map(^n).max
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,479 ⟶ 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,538 ⟶ 1,966:
}
}
main</langsyntaxhighlight>
 
{{out}}
Line 1,563 ⟶ 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,600 ⟶ 2,028:
break
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,622 ⟶ 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,635 ⟶ 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,643 ⟶ 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,663 ⟶ 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