Stirling numbers of the second kind: Difference between revisions

added RPL
m (syntax highlighting fixup automation)
(added RPL)
 
(10 intermediate revisions by 4 users not shown)
Line 104:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses the LONG LONG INT mode of Algol 68g which allows large precision integers. As the default precision of LONG LONG INT is too small, the precision is specified via a pragmatic comment.
<syntaxhighlight lang="algol68">BEGIN
BEGIN # show some Stirling numbers of the second kind #
 
# specify the precision of LONG LONG INT, somewhat under 160 digits are #
Line 132:
print( ( "Stirling numbers of the second kind:", newline ) );
print( ( " k" ) );
FOR k FROM 0 TO max stirling DO print( ( whole( k, -108 ) ) ) OD;
print( ( newline, " n", newline ) );
FOR n FROM 0 TO max stirling DO
print( ( whole( n, -2 ) ) );
FOR k FROM 0 TO n DO
print( ( whole( s2[ n, k ], -108 ) ) )
OD;
print( ( newline ) )
Line 153:
print( ( whole( max 100, 0 ), newline ) )
END
END
END</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Stirling numbers of the second kind:
k 0 1 2 3 4 5 6 7 8 9 10 11 12
n
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
Maximum Stirling number of the second kind with n = 100:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
Line 195 ⟶ 196:
write( "Stirling numbers of the second kind:" );
write( " k" );
for k := 0 until MAX_STIRLING do writeon( i_w := 108, s_w := 0, k );
write( " n" );
for n := 0 until MAX_STIRLING do begin
write( i_w := 2, s_w := 0, n );
for k := 0 until n do writeon( i_w := 108, s_w := 0, s2( n, k ) );
end for_n
end
end.
end.</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Stirling numbers of the second kind:
k 0 1 2 3 4 5 6 7 8 9 10 11 12
n
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
</pre>
 
Line 491 ⟶ 493:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
print "Unsigned Stirling numbers of the second kind:"
len a[] 13 ; arrbase a[] 0
len b[] 13 ; arrbase b[] 0
a[0] = 1
print 1
for n = 1 to 12
b[0] = 0
write 0 & " "
for k = 1 to n - 1
b[k] = k * a[k] + a[k - 1]
write b[k] & " "
.
b[n] = 1
write 1 & " "
print ""
swap a[] b[]
.
</syntaxhighlight>
 
=={{header|Factor}}==
Line 590 ⟶ 613:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Stirling_numbers_of_the_second_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'''
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.
 
'''Version 1. Recursive'''
In '''[https://formulae.org/?example=Stirling_numbers_of_the_second_kind this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Stirling numbers of the second kind 01.png]]
 
'''Test case 1. Show the Stirling numbers of the second kind, S₂(n, k), up to S₂(12, 12)'''
 
[[File:Fōrmulæ - Stirling numbers of the second kind 02.png]]
 
[[File:Fōrmulæ - Stirling numbers of the second kind 03.png]]
 
'''Version 2. Non recursive'''
 
A faster, non recursive version is presented. This constructs a matrix.
 
[[File:Fōrmulæ - Stirling numbers of the second kind 04.png]]
 
'''Test case 1. Show the Stirling numbers of the second kind, S₂(n, k), up to S₂(12, 12)'''
 
[[File:Fōrmulæ - Stirling numbers of the second kind 05.png]]
 
(the result is the same as recursive version)
 
'''Test case 2. Find the maximum value of S₂(n, k) where n ≤ 100'''
 
[[File:Fōrmulæ - Stirling numbers of the second kind 06.png]]
 
[[File:Fōrmulæ - Stirling numbers of the second kind 07.png]]
 
=={{header|Go}}==
Line 1,059 ⟶ 1,108:
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
(115 digits, k = 28)</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- show some Stirling numbers of the second kind
local MAX_STIRLING = 12;
-- construct a matrix of Stirling numbers up to max n, max n
local s2 = {}
for n = 0, MAX_STIRLING do
s2[ n ] = {}
for k = 0, MAX_STIRLING do s2[ n ][ k ] = 0 end
end
for n = 0, MAX_STIRLING do s2[ n ][ n ] = 1 end
for n = 0, MAX_STIRLING - 1 do
for k = 1, n do
s2[ n + 1 ][ k ] = k * s2[ n ][ k ] + s2[ n ][ k - 1 ]
end
end
io.write( "Stirling numbers of the second kind:\n" )
io.write( " k" )
for k = 0, MAX_STIRLING do io.write( string.format( "%8d", k ) ) end
io.write( "\n" )
io.write( " n\n" );
for n = 0, MAX_STIRLING do
io.write( string.format( "%2d", n ) )
for k = 0, n do io.write( string.format( "%8d", s2[ n ][ k ] ) ) end
io.write( "\n" )
end
end
</syntaxhighlight>
{{out}}
<pre>
Stirling numbers of the second kind:
k 0 1 2 3 4 5 6 7 8 9 10 11 12
n
0 1
1 0 1
2 0 1 1
3 0 1 3 1
4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
9 0 1 255 3025 7770 6951 2646 462 36 1
10 0 1 511 9330 34105 42525 22827 5880 750 45 1
11 0 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1
12 0 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 1,545 ⟶ 1,642:
The maximum value (which has 115 decimal digits):
7769730053598745155212806612787584787397878128370115840974992570102386086289805848025074822404843545178960761551674
</pre>
 
=={{header|RPL}}==
{{works with|RPL|HP49-C}}
« '''IF''' DUP2 AND NOT '''THEN''' ==
'''ELSE'''
SWAP 1 - OVER
DUP2 1 - <span style="color:blue">S2</span> 4 ROLLD <span style="color:blue">S2</span> * +
'''END'''
» '<span style="color:blue">S2</span>' STO <span style="color:grey">''@ ( n k → S2(n,k) )''</span>
 
12 12 « <span style="color:blue">S2</span> » LCXM
{{out}}
<pre>
1: [[ 1 0 0 0 0 0 0 0 0 0 0 0 ]
[ 1 1 0 0 0 0 0 0 0 0 0 0 ]
[ 1 3 1 0 0 0 0 0 0 0 0 0 ]
[ 1 7 6 1 0 0 0 0 0 0 0 0 ]
[ 1 15 25 10 1 0 0 0 0 0 0 0 ]
[ 1 31 90 65 15 1 0 0 0 0 0 0 ]
[ 1 63 301 350 140 21 1 0 0 0 0 0 ]
[ 1 127 966 1701 1050 266 28 1 0 0 0 0 ]
[ 1 255 3025 7770 6951 2646 462 36 1 0 0 0 ]
[ 1 511 9330 34105 42525 22827 5880 750 45 1 0 0 ]
[ 1 1023 28501 145750 246730 179487 63987 11880 1155 55 1 0 ]
[ 1 2047 86526 611501 1379400 1323652 627396 159027 22275 1705 66 1 ]]
</pre>
 
Line 1,594 ⟶ 1,717:
 
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func S2(n, k) { # Stirling numbers of the second kind
Line 1,814 ⟶ 1,938:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
var computed = {}
1,150

edits