Stirling numbers of the second kind: Difference between revisions

added RPL
m (→‎{{header|Wren}}: Minor tidy)
(added RPL)
 
(4 intermediate revisions by 2 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>
{{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>
{{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 621 ⟶ 623:
'''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 02a02.png]]
 
[[File:Fōrmulæ - Stirling numbers of the second kind 03.png]]
Line 633 ⟶ 635:
'''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 05a05.png]]
 
(the result is the same as recursive version)
Line 1,106 ⟶ 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,592 ⟶ 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,641 ⟶ 1,717:
 
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func S2(n, k) { # Stirling numbers of the second kind
1,150

edits