Bell numbers: Difference between revisions
m
→{{header|ALGOL W}}: fixed trans
m (→{{header|ALGOL W}}: fixed trans) |
|||
(63 intermediate revisions by 29 users not shown) | |||
Line 31:
:* '''[[oeis:A000110|OEIS:A000110 Bell or exponential numbers]]'''
:* '''[[oeis:A011971|OEIS:A011971 Aitken's array]]'''
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F bellTriangle(n)
[[BigInt]] tri
L(i) 0 .< n
tri.append([BigInt(0)] * i)
tri[1][0] = 1
L(i) 2 .< n
tri[i][0] = tri[i - 1][i - 2]
L(j) 1 .< i
tri[i][j] = tri[i][j - 1] + tri[i - 1][j - 1]
R tri
V bt = bellTriangle(51)
print(‘First fifteen and fiftieth Bell numbers:’)
L(i) 1..15
print(‘#2: #.’.format(i, bt[i][0]))
print(‘50: ’bt[50][0])
print()
print(‘The first ten rows of Bell's triangle:’)
L(i) 1..10
print(bt[i])</syntaxhighlight>
{{out}}
<pre>
First fifteen and fiftieth Bell numbers:
1: 1
2: 1
3: 2
4: 5
5: 15
6: 52
7: 203
8: 877
9: 4140
10: 21147
11: 115975
12: 678570
13: 4213597
14: 27644437
15: 190899322
50: 10726137154573358400342215518590002633917247281
The first ten rows of Bell's triangle:
[1]
[1, 2]
[2, 3, 5]
[5, 7, 10, 15]
[15, 20, 27, 37, 52]
[52, 67, 87, 114, 151, 203]
[203, 255, 322, 409, 523, 674, 877]
[877, 1080, 1335, 1657, 2066, 2589, 3263, 4140]
[4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147]
[21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975]
</pre>
=={{header|Ada}}==
{{works with|GNAT|8.3.0}}
<syntaxhighlight lang="ada">
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Bell_Triangle is array(Positive range <>, Positive range <>) of Natural;
procedure Print_Rows (Row : in Positive; Triangle : in Bell_Triangle) is
begin
if Row in Triangle'Range(1) then
for I in Triangle'First(1) .. Row loop
Put_Line (Triangle (I, 1)'Image);
end loop;
end if;
end Print_Rows;
procedure Print_Triangle (Num : in Positive; Triangle : in Bell_Triangle) is
begin
if Num in Triangle'Range then
for I in Triangle'First(1) .. Num loop
for J in Triangle'First(2) .. Num loop
if Triangle (I, J) /= 0 then
Put (Triangle (I, J)'Image);
end if;
end loop;
New_Line;
end loop;
end if;
end Print_Triangle;
procedure Bell_Numbers is
Triangle : Bell_Triangle(1..15, 1..15) := (Others => (Others => 0));
Temp : Positive := 1;
begin
Triangle (1, 1) := 1;
for I in Triangle'First(1) + 1 .. Triangle'Last(1) loop
Triangle (I, 1) := Temp;
for J in Triangle'First(2) .. Triangle'Last(2) - 1 loop
if Triangle (I - 1, J) /= 0 then
Triangle (I, J + 1) := Triangle (I, J) + Triangle (I - 1, J);
else
Temp := Triangle (I, J);
exit;
end if;
end loop;
end loop;
Put_Line ("First 15 Bell numbers:");
Print_Rows (15, Triangle);
New_Line;
Put_Line ("First 10 rows of the Bell triangle:");
Print_Triangle (10, Triangle);
end Bell_Numbers;
begin
Bell_Numbers;
end Main;
</syntaxhighlight>
{{out}}
<pre>
First 15 Bell numbers:
1
1
2
5
15
52
203
877
4140
21147
115975
678570
4213597
27644437
190899322
First 10 rows of the Bell triangle:
1
1 2
2 3 5
5 7 10 15
15 20 27 37 52
52 67 87 114 151 203
203 255 322 409 523 674 877
877 1080 1335 1657 2066 2589 3263 4140
4140 5017 6097 7432 9089 11155 13744 17007 21147
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975
</pre>
=={{header|ALGOL 68}}==
{{Trans|Delphi}}
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses Algol 68G's LONG LONG INT to calculate the numbers up to 50. Calculates the numbers using the triangle algorithm but without storing the triangle as a whole - each line of the triangle replaces the previous one.
<syntaxhighlight lang="algol68">BEGIN # show some Bell numbers #
PROC show bell = ( INT n, LONG LONG INT bell number )VOID:
print( ( whole( n, -2 ), ": ", whole( bell number, 0 ), newline ) );
INT max bell = 50;
[ 0 : max bell - 2 ]LONG LONG INT a; FOR i TO UPB a DO a[ i ] := 0 OD;
a[ 0 ] := 1;
show bell( 1, a[ 0 ] );
FOR n FROM 0 TO UPB a DO
# replace a with the next line of the triangle #
a[ n ] := a[ 0 ];
FOR j FROM n BY -1 TO 1 DO
a[ j - 1 ] +:= a[ j ]
OD;
IF n < 14 THEN
show bell( n + 2, a[ 0 ] )
ELIF n = UPB a THEN
print( ( "...", newline ) );
show bell( n + 2, a[ 0 ] )
FI
OD
END</syntaxhighlight>
{{out}}
<pre>
1: 1
2: 1
3: 2
4: 5
5: 15
6: 52
7: 203
8: 877
9: 4140
10: 21147
11: 115975
12: 678570
13: 4213597
14: 27644437
15: 190899322
...
50: 10726137154573358400342215518590002633917247281
</pre>
=={{header|ALGOL-M}}==
<syntaxhighlight lang="algolm">begin
integer function index(row, col);
integer row, col;
index := row * (row-1)/ 2 + col;
integer ROWS; ROWS := 15;
begin
decimal(11) array bell[0:ROWS*(ROWS+1)/2];
integer i, j;
bell[index(1, 0)] := 1.;
for i := 2 step 1 until ROWS do
begin
bell[index(i, 0)] := bell[index(i-1, i-2)];
for j := 1 step 1 until i-1 do
bell[index(i,j)] := bell[index(i,j-1)] + bell[index(i-1,j-1)];
end;
write("First fifteen Bell numbers:");
for i := 1 step 1 until ROWS do
begin
write(i);
writeon(": ");
writeon(bell[index(i,0)]);
end;
write("");
write("First ten rows of Bell's triangle:");
for i := 1 step 1 until 10 do
begin
write("");
for j := 0 step 1 until i-1 do
writeon(bell[index(i,j)]);
end;
end;
end</syntaxhighlight>
{{out}}
<pre>First fifteen Bell numbers:
1: 1.0
2: 1.0
3: 2.0
4: 5.0
5: 15.0
6: 52.0
7: 203.0
8: 877.0
9: 4140.0
10: 21147.0
11: 115975.0
12: 678570.0
13: 4213597.0
14: 27644437.0
15: 190899322.0
First ten rows of Bell's triangle:
1.0
1.0 2.0
2.0 3.0 5.0
5.0 7.0 10.0 15.0
15.0 20.0 27.0 37.0 52.0
52.0 67.0 87.0 114.0 151.0 203.0
203.0 255.0 322.0 409.0 523.0 674.0 877.0
877.0 1080.0 1335.0 1657.0 2066.0 2589.0 3263.0 4140.0
4140.0 5017.0 6097.0 7432.0 9089.0 11155.0 13744.0 17007.0 21147.0
21147.0 25287.0 30304.0 36401.0 43833.0 52922.0 64077.0 77821.0 94828.0 115975.0</pre>
=={{header|ALGOL W}}==
{{Trans|ALGOL 68|First 15 numbers only}}
<syntaxhighlight lang="algolw">
begin % show some Bell numbers %
integer MAX_BELL;
MAX_BELL := 15;
begin
procedure showBell ( integer value n, bellNumber ) ;
write( i_w := 2, s_w := 0, n, ": ", i_w := 1, bellNumber );
integer array a ( 0 :: MAX_BELL - 2 );
for i := 1 until MAX_BELL - 2 do a( i ) := 0;
a( 0 ) := 1;
showBell( 1, a( 0 ) );
for n := 0 until MAX_BELL - 2 do begin
% replace a with the next line of the triangle %
a( n ) := a( 0 );
for j := n step -1 until 1 do a( j - 1 ) := a( j - 1 ) + a( j );
showBell( n + 2, a( 0 ) )
end for_n
end
end.
</syntaxhighlight>
{{out}}
<pre>
1: 1
2: 1
3: 2
4: 5
5: 15
6: 52
7: 203
8: 877
9: 4140
10: 21147
11: 115975
12: 678570
13: 4213597
14: 27644437
15: 190899322
</pre>
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">bell←{
tr←↑(⊢,(⊂⊃∘⌽+0,+\)∘⊃∘⌽)⍣14⊢,⊂,1
⎕←'First 15 Bell numbers:'
⎕←tr[;1]
⎕←'First 10 rows of Bell''s triangle:'
⎕←tr[⍳10;⍳10]
}</syntaxhighlight>
{{out}}
<pre>First 15 Bell numbers:
1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322
First 10 rows of Bell's triangle:
1 0 0 0 0 0 0 0 0 0
1 2 0 0 0 0 0 0 0 0
2 3 5 0 0 0 0 0 0 0
5 7 10 15 0 0 0 0 0 0
15 20 27 37 52 0 0 0 0 0
52 67 87 114 151 203 0 0 0 0
203 255 322 409 523 674 877 0 0 0
877 1080 1335 1657 2066 2589 3263 4140 0 0
4140 5017 6097 7432 9089 11155 13744 17007 21147 0
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975</pre>
=={{header|Arturo}}==
{{trans|D}}
<syntaxhighlight lang="rebol">bellTriangle: function[n][
tri: map 0..n-1 'x [ map 0..n 'y -> 0 ]
set get tri 1 0 1
loop 2..n-1 'i [
set get tri i 0 get (get tri i-1) i-2
loop 1..i-1 'j [
set get tri i j (get (get tri i) j-1) + ( get (get tri i-1) j-1)
]
]
return tri
]
bt: bellTriangle 51
loop 1..15 'x ->
print [x "=>" first bt\[x]]
print ["50 =>" first last bt]
print ""
print "The first ten rows of Bell's triangle:"
loop 1..10 'i ->
print filter bt\[i] => zero?</syntaxhighlight>
{{out}}
<pre>1 => 1
2 => 1
3 => 2
4 => 5
5 => 15
6 => 52
7 => 203
8 => 877
9 => 4140
10 => 21147
11 => 115975
12 => 678570
13 => 4213597
14 => 27644437
15 => 190899322
50 => 10726137154573358400342215518590002633917247281
The first ten rows of Bell's triangle:
1
1 2
2 3 5
5 7 10 15
15 20 27 37 52
52 67 87 114 151 203
203 255 322 409 523 674 877
877 1080 1335 1657 2066 2589 3263 4140
4140 5017 6097 7432 9089 11155 13744 17007 21147
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975 </pre>
=={{header|AutoHotkey}}==
<
Bell_triangle(maxRows){
row := 1, col := 1, Arr := []
Line 64 ⟶ 449:
return Trim(res, "`n")
}
;-----------------------------------</
Examples:<
MsgBox % Show_Bell_triangle(Bell_triangle(10))
return</
{{out}}
<pre>1
Line 95 ⟶ 480:
4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147
21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975</pre>
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|QuickBASIC}}
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 REM Bell numbers
110 LET MaxN = 14
120 OPTION BASE 0
130 DIM A(13) ! i.e. DIM A(MaxN - 1), ANSI BASIC does not allow expressions in the bound arguments.
140 FOR I = 0 TO MaxN - 1
150 LET A(I) = 0
160 NEXT I
170 LET N = 0
180 LET A(0) = 1
190 PRINT USING "B(##) = #########": N, A(0)
200 DO WHILE N < MaxN
210 LET A(N) = A(0)
220 FOR J = N TO 1 STEP -1
230 LET A(J - 1) = A(J - 1) + A(J)
240 NEXT J
250 LET N = N + 1
260 PRINT USING "B(##) = #########": N, A(0)
270 LOOP
280 END
</syntaxhighlight>
{{out}}
<pre>
B( 0) = 1
B( 1) = 1
B( 2) = 2
B( 3) = 5
B( 4) = 15
B( 5) = 52
B( 6) = 203
B( 7) = 877
B( 8) = 4140
B( 9) = 21147
B(10) = 115975
B(11) = 678570
B(12) = 4213597
B(13) = 27644437
B(14) = 190899322
</pre>
==={{header|Applesoft BASIC}}===
{{trans|C}}
<syntaxhighlight lang="gwbasic"> 100 LET ROWS = 15
110 LET M$ = CHR$ (13)
120 LET N = ROWS: GOSUB 500"BELLTRIANGLE"
130 PRINT "FIRST FIFTEEN BELL NUMBERS:"
140 FOR I = 1 TO ROWS
150 LET BR = I:BC = 0: GOSUB 350"GETBELL"
160 HTAB T * 13 + 1
170 PRINT RIGHT$ (" " + STR$ (I),2)": "BV; MID$ (M$,1,T = 2);
180 LET T = T + 1 - (T = 2) * 3
190 NEXT I
200 PRINT M$"THE FIRST TEN ROWS OF BELL'S TRIANGLE:";
210 FOR I = 1 TO 10
220 LET BR = I:BC = 0: GOSUB 350"GETBELL"
230 PRINT M$BV;
240 FOR J = 1 TO I - 1
250 IF I - 1 > = J THEN BR = I:BC = J: GOSUB 350"GETBELL": PRINT " "BV;
260 NEXT J,I
270 END
300 LET BI = BR * (BR - 1) / 2 + BC
310 RETURN
350 GOSUB 300"BELLINDEX"
360 LET BV = TRI(BI)
370 RETURN
400 GOSUB 300"BELLINDEX"
410 LET TRI(BI) = BV
420 RETURN
500 DIM TRI(N * (N + 1) / 2)
510 LET BR = 1:BC = 0:BV = 1: GOSUB 400"SETBELL"
520 FOR I = 2 TO N
530 LET BR = I - 1:BC = I - 2: GOSUB 350"GETBELL"
540 LET BR = I:BC = 0: GOSUB 400"SETBELL"
550 FOR J = 1 TO I - 1
560 LET BR = I:BC = J - 1: GOSUB 350"GETBELL":V = BV
570 LET BR = I - 1:BC = J - 1: GOSUB 350"GETBELL"
580 LET BR = I:BC = J:BV = V + BV: GOSUB 400"SETBELL"
590 NEXT J,I
600 RETURN</syntaxhighlight>
==={{header|ASIC}}===
{{trans|Delphi}}
Compile with the ''Extended math'' option.
<syntaxhighlight lang="basic">
REM Bell numbers
DIM A&(13)
FOR I = 0 TO 13
A&(I) = 0
NEXT I
N = 0
A&(0) = 1
GOSUB DisplayRow:
WHILE N <= 13
A&(N) = A&(0)
J = N
WHILE J >= 1
JM1 = J - 1
A&(JM1) = A&(JM1) + A&(J)
J = J - 1
WEND
N = N + 1
GOSUB DisplayRow:
WEND
END
DisplayRow:
PRINT "B(";
SN$ = STR$(N)
SN$ = RIGHT$(SN$, 2)
PRINT SN$;
PRINT ") =";
PRINT A&(0)
RETURN
</syntaxhighlight>
{{out}}
<pre>
B( 0) = 1
B( 1) = 1
B( 2) = 2
B( 3) = 5
B( 4) = 15
B( 5) = 52
B( 6) = 203
B( 7) = 877
B( 8) = 4140
B( 9) = 21147
B(10) = 115975
B(11) = 678570
B(12) = 4213597
B(13) = 27644437
B(14) = 190899322
</pre>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|ASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 dim a(13)
120 for i = 0 to ubound(a) : a(i) = 0 : next i
130 n = 0
140 a(0) = 1
150 displayrow()
160 while n <= ubound(a)
170 a(n) = a(0)
180 j = n
190 while j >= 1
200 jm1 = j-1
210 a(jm1) = a(jm1)+a(j)
220 j = j-1
230 wend
240 n = n+1
250 displayrow()
260 wend
270 end
280 sub displayrow()
290 print "B(";
300 print right$(str$(n),2)") = " a(0)
310 return</syntaxhighlight>
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#define MAX 21
#macro ncp(n, p)
(fact(n)/(fact(p))/(fact(n-p)))
#endmacro
dim as ulongint fact(0 to MAX), bell(0 to MAX)
dim as uinteger n=0, k
fact(0) = 1
for k=1 to MAX
fact(k) = k*fact(k-1)
next k
bell(n) = 1
print n, bell(n)
for n=0 to MAX-1
for k=0 to n
bell(n+1) += ncp(n, k)*bell(k)
next k
print n+1, bell(n+1)
next n</syntaxhighlight>
==={{header|GW-BASIC}}===
{{works with|Chipmunk Basic}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{trans|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">100 CLS
110 DIM A#(13)
120 FOR I = 0 TO UBOUND(A#) : A#(I) = 0 : NEXT I
130 N = 0
140 A#(0) = 1
150 GOSUB 280
160 WHILE N <= 13
170 A#(N) = A#(0)
180 J = N
190 WHILE J >= 1
200 JM1 = J-1
210 A#(JM1) = A#(JM1)+A#(J)
220 J = J-1
230 WEND
240 N = N+1
250 GOSUB 280
260 WEND
270 END
280 REM Display Row
290 PRINT "B(";
300 PRINT RIGHT$(STR$(N),2)") = " A#(0)
310 RETURN</syntaxhighlight>
==={{header|MSX Basic}}===
{{trans|Applesoft BASIC}}
<syntaxhighlight lang="qbasic">100 ROWS = 15
110 M$ = CHR$(13)
120 N = ROWS: GOSUB 500
130 PRINT "FIRST FIFTEEN BELL NUMBERS:"
140 FOR I = 1 TO ROWS
150 BR = I: BC = 0: GOSUB 350
160 PRINT RIGHT$(" " + STR$(I),2); ": "; BV; MID$(M$,1,2)
170 T = T + 1 - (T = 2) * 3
180 NEXT I
190 PRINT
200 PRINT "THE FIRST 10 ROWS OF BELL'S TRIANGLE:";
210 FOR I = 1 TO 10
220 BR = I: BC = 0: GOSUB 350
230 PRINT M$: PRINT BV;
240 FOR J = 1 TO I - 1
250 IF I - 1 >= J THEN BR = I: BC = J: GOSUB 350: PRINT BV;
260 NEXT J, I
270 END
300 BI = BR * (BR-1) / 2 + BC
310 RETURN
350 GOSUB 300
360 BV = TRI(BI)
370 RETURN
400 GOSUB 300
410 TRI(BI) = BV
420 RETURN
500 DIM TRI(N * (N+1) / 2)
510 BR = 1: BC = 0: BV = 1: GOSUB 400
520 FOR I = 2 TO N
530 BR = I - 1: BC = I - 2: GOSUB 350
540 BR = I: BC = 0: GOSUB 400
550 FOR J = 1 TO I - 1
560 BR = I: BC = J - 1: GOSUB 350: V = BV
570 BR = I - 1: BC = J - 1: GOSUB 350
580 BR = I: BC = J: BV = V + BV: GOSUB 400
590 NEXT J, I
600 RETURN</syntaxhighlight>
==={{header|QuickBASIC}}===
{{works with|QBasic|1.1}}
{{trans|Delphi}}
<syntaxhighlight lang="qbasic">
' Bell numbers
CONST MAXN% = 14
DIM A&(MAXN% - 1)
FOR I% = 0 TO MAXN% - 1
A&(I%) = 0
NEXT I%
N% = 0
A&(0) = 1
PRINT USING "B(##) = #########"; N%; A&(0)
WHILE N% < MAXN%
A&(N%) = A&(0)
FOR J% = N% TO 1 STEP -1
A&(J% - 1) = A&(J% - 1) + A&(J%)
NEXT J%
N% = N% + 1
PRINT USING "B(##) = #########"; N%; A&(0)
WEND
END
</syntaxhighlight>
{{out}}
<pre>
B( 0) = 1
B( 1) = 1
B( 2) = 2
B( 3) = 5
B( 4) = 15
B( 5) = 52
B( 6) = 203
B( 7) = 877
B( 8) = 4140
B( 9) = 21147
B(10) = 115975
B(11) = 678570
B(12) = 4213597
B(13) = 27644437
B(14) = 190899322
</pre>
==={{header|RapidQ}}===
{{trans|Delphi}}
{{trans|QuickBASIC|Translated only display statements, the rest is the same.}}
<syntaxhighlight lang="basic">
' Bell numbers
CONST MAXN% = 14
DIM A&(MAXN% - 1)
FOR I% = 0 TO MAXN% - 1
A&(I%) = 0
NEXT I%
N% = 0
A&(0) = 1
PRINT FORMAT$("B(%2d) = %9d", N%, A&(0))
WHILE N% < MAXN%
A&(N%) = A&(0)
FOR J% = N% TO 1 STEP -1
A&(J% - 1) = A&(J% - 1) + A&(J%)
NEXT J%
N% = N% + 1
PRINT FORMAT$("B(%2d) = %9d", N%, A&(0))
WEND
END
</syntaxhighlight>
{{out}}
<pre>
B( 0) = 1
B( 1) = 1
B( 2) = 2
B( 3) = 5
B( 4) = 15
B( 5) = 52
B( 6) = 203
B( 7) = 877
B( 8) = 4140
B( 9) = 21147
B(10) = 115975
B(11) = 678570
B(12) = 4213597
B(13) = 27644437
B(14) = 190899322
</pre>
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Numerics
Imports System.Runtime.CompilerServices
Module Module1
<Extension()>
Sub Init(Of T)(array As T(), value As T)
If IsNothing(array) Then Return
For i = 0 To array.Length - 1
array(i) = value
Next
End Sub
Function BellTriangle(n As Integer) As BigInteger()()
Dim tri(n - 1)() As BigInteger
For i = 0 To n - 1
Dim temp(i - 1) As BigInteger
tri(i) = temp
tri(i).Init(0)
Next
tri(1)(0) = 1
For i = 2 To n - 1
tri(i)(0) = tri(i - 1)(i - 2)
For j = 1 To i - 1
tri(i)(j) = tri(i)(j - 1) + tri(i - 1)(j - 1)
Next
Next
Return tri
End Function
Sub Main()
Dim bt = BellTriangle(51)
Console.WriteLine("First fifteen Bell numbers:")
For i = 1 To 15
Console.WriteLine("{0,2}: {1}", i, bt(i)(0))
Next
Console.WriteLine("50: {0}", bt(50)(0))
Console.WriteLine()
Console.WriteLine("The first ten rows of Bell's triangle:")
For i = 1 To 10
Dim it = bt(i).GetEnumerator()
Console.Write("[")
If it.MoveNext() Then
Console.Write(it.Current)
End If
While it.MoveNext()
Console.Write(", ")
Console.Write(it.Current)
End While
Console.WriteLine("]")
Next
End Sub
End Module</syntaxhighlight>
{{out}}
<pre>First fifteen Bell numbers:
1: 1
2: 1
3: 2
4: 5
5: 15
6: 52
7: 203
8: 877
9: 4140
10: 21147
11: 115975
12: 678570
13: 4213597
14: 27644437
15: 190899322
50: 10726137154573358400342215518590002633917247281
The first ten rows of Bell's triangle:
[1]
[1, 2]
[2, 3, 5]
[5, 7, 10, 15]
[15, 20, 27, 37, 52]
[52, 67, 87, 114, 151, 203]
[203, 255, 322, 409, 523, 674, 877]
[877, 1080, 1335, 1657, 2066, 2589, 3263, 4140]
[4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147]
[21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975]</pre>
=={{header|C}}==
{{trans|D}}
<
#include <stdlib.h>
Line 154 ⟶ 969:
free(bt);
return 0;
}</
{{out}}
Line 186 ⟶ 1,001:
21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975</pre>
=={{header|C
{{trans|D}}
<
using System.Numerics;
Line 242 ⟶ 1,057:
}
}
}</
{{out}}
<pre>First fifteen and fiftieth Bell numbers:
Line 277 ⟶ 1,092:
{{libheader|Boost}}
Requires C++14 or later. If HAVE_BOOST is defined, we use the cpp_int class from Boost so we can display the 50th Bell number, as shown in the output section below.
<
#include <vector>
Line 327 ⟶ 1,142:
}
return 0;
}</
{{out}}
Line 362 ⟶ 1,177:
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975
</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">bell = cluster is make, get
rep = array[int]
idx = proc (row, col: int) returns (int)
return (row * (row - 1) / 2 + col)
end idx
get = proc (tri: cvt, row, col: int) returns (int)
return (tri[idx(row, col)])
end get
make = proc (rows: int) returns (cvt)
length: int := rows * (rows+1) / 2
arr: rep := rep$fill(0, length, 0)
arr[idx(1,0)] := 1
for i: int in int$from_to(2, rows) do
arr[idx(i,0)] := arr[idx(i-1, i-2)]
for j: int in int$from_to(1, i-1) do
arr[idx(i,j)] := arr[idx(i,j-1)] + arr[idx(i-1,j-1)]
end
end
return(arr)
end make
end bell
start_up = proc ()
rows = 15
po: stream := stream$primary_output()
belltri: bell := bell$make(rows)
stream$putl(po, "The first 15 Bell numbers are:")
for i: int in int$from_to(1, rows) do
stream$putl(po, int$unparse(i)
|| ": " || int$unparse(bell$get(belltri, i, 0)))
end
stream$putl(po, "\nThe first 10 rows of the Bell triangle:")
for row: int in int$from_to(1, 10) do
for col: int in int$from_to(0, row-1) do
stream$putright(po, int$unparse(bell$get(belltri, row, col)), 7)
end
stream$putc(po, '\n')
end
end start_up</syntaxhighlight>
{{out}}
<pre>The first 15 Bell numbers are:
1: 1
2: 1
3: 2
4: 5
5: 15
6: 52
7: 203
8: 877
9: 4140
10: 21147
11: 115975
12: 678570
13: 4213597
14: 27644437
15: 190899322
The first 10 rows of the Bell triangle:
1
1 2
2 3 5
5 7 10 15
15 20 27 37 52
52 67 87 114 151 203
203 255 322 409 523 674 877
877 1080 1335 1657 2066 2589 3263 4140
4140 5017 6097 7432 9089 11155 13744 17007 21147
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975</pre>
=={{header|Common Lisp}}==
===via Bell triangle===
<
;; triangle's row; the last row is at the head of the list.
(defun grow-triangle (triangle)
Line 425 ⟶ 1,317:
(format t "The first 10 rows of Bell triangle:~%")
(print-bell-triangle (make-triangle 10))</
{{out}}
<pre>B_0 (first Bell number) = 1
Line 464 ⟶ 1,356:
===via Stirling numbers of the second kind===
This solution's algorithm is substantially slower than the algorithm based on the Bell triangle, because of the many nested loops.
<
;; Compute the factorial
Line 506 ⟶ 1,398:
;; Final invocation
(loop for n in *numbers-to-print* do
(print-bell-number n (bell n)))</
{{out}}
<pre>B_0 (first Bell number) = 1
Line 530 ⟶ 1,422:
B_49 (fiftieth Bell number) = 10,726,137,154,573,358,400,342,215,518,590,002,633,917,247,281
B_50 (fifty-first Bell number) = 185,724,268,771,078,270,438,257,767,181,908,917,499,221,852,770</pre>
=={{header|Cowgol}}==
{{trans|C}}
<syntaxhighlight lang="cowgol">include "cowgol.coh";
typedef B is uint32;
typedef I is intptr;
sub bellIndex(row: I, col: I): (addr: I) is
addr := (row * (row - 1) / 2 + col) * @bytesof B;
end sub;
sub getBell(row: I, col: I): (bell: B) is
bell := [LOMEM as [B] + bellIndex(row, col)];
end sub;
sub setBell(row: I, col: I, bell: B) is
[LOMEM as [B] + bellIndex(row, col)] := bell;
end sub;
sub bellTriangle(n: I) is
var length := n * (n + 1) / 2;
var bytes := length * @bytesof B;
if HIMEM - LOMEM < bytes then
print("not enough memory\n");
ExitWithError();
end if;
MemZero(LOMEM, bytes);
setBell(1, 0, 1);
var i: I := 2;
while i <= n loop
setBell(i, 0, getBell(i-1, i-2));
var j: I := 1;
while j < i loop
var value := getBell(i, j-1) + getBell(i-1, j-1);
setBell(i, j, value);
j := j + 1;
end loop;
i := i + 1;
end loop;
end sub;
const ROWS := 15;
bellTriangle(ROWS);
print("First fifteen Bell numbers:\n");
var i: I := 1;
while i <= ROWS loop
print_i32(i as uint32);
print(": ");
print_i32(getBell(i, 0) as uint32);
print_nl();
i := i + 1;
end loop;
print("\nThe first ten rows of Bell's triangle:\n");
i := 1;
while i <= 10 loop
var j: I := 0;
loop
print_i32(getBell(i, j) as uint32);
j := j + 1;
if j == i then break;
else print(", ");
end if;
end loop;
i := i + 1;
print_nl();
end loop;</syntaxhighlight>
{{out}}
<pre>First fifteen Bell numbers:
1: 1
2: 1
3: 2
4: 5
5: 15
6: 52
7: 203
8: 877
9: 4140
10: 21147
11: 115975
12: 678570
13: 4213597
14: 27644437
15: 190899322
The first ten rows of Bell's triangle:
1
1, 2
2, 3, 5
5, 7, 10, 15
15, 20, 27, 37, 52
52, 67, 87, 114, 151, 203
203, 255, 322, 409, 523, 674, 877
877, 1080, 1335, 1657, 2066, 2589, 3263, 4140
4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147
21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975</pre>
=={{header|D}}==
{{trans|Go}}
<
import std.bigint;
import std.stdio : writeln, writefln;
Line 565 ⟶ 1,559:
writeln(bt[i]);
}
}</
{{out}}
<pre>First fifteen and fiftieth Bell numbers:
Line 601 ⟶ 1,595:
It shows a way of calculating Bell numbers without using a triangle.
Output numbering is as in the statement of the task, namely B_0 = 1, B_1 = 1, B_2 = 2, ....
<syntaxhighlight lang="delphi">
program BellNumbers;
Line 613 ⟶ 1,607:
const
var
n : integer; // index of Bell number
j : integer; // loop variable
a : array [0..
{ Subroutine to display that a[0] is the Bell number B_n }
Line 630 ⟶ 1,624:
a[0] := 1;
Display(); // some programmers would prefer Display;
while (n <
a[n] := a[0];
for j := n downto 1 do inc( a[j - 1], a[j]);
Line 637 ⟶ 1,631:
end;
end.
</syntaxhighlight>
{{out}}
<pre>
Line 667 ⟶ 1,661:
B_25 = 4638590332229999353
</pre>
=={{header|EasyLang}}==
{{trans|Julia}}
<syntaxhighlight lang="easylang">
func bell n .
len list[] n
list[1] = 1
for i = 2 to n
for j = 1 to i - 2
list[i - j - 1] += list[i - j]
.
list[i] = list[1] + list[i - 1]
.
return list[n]
.
for i = 1 to 15
print bell i
.
</syntaxhighlight>
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
defmodule Bell do
def triangle(), do: Stream.iterate([1], fn l -> bell_row l, [List.last l] end)
Line 686 ⟶ 1,699:
IO.puts "THe first 10 rows of Bell's triangle:"
IO.inspect(Bell.triangle() |> Enum.take(10))
</syntaxhighlight>
{{Out}}
<pre>
Line 711 ⟶ 1,724:
=={{header|F_Sharp|F#}}==
===The function===
<
// Generate bell triangle. Nigel Galloway: July 6th., 2019
let bell=Seq.unfold(fun g->Some(g,List.scan(+) (List.last g) g))[1I]
</syntaxhighlight>
===The Task===
<
bell|>Seq.take 10|>Seq.iter(printfn "%A")
</syntaxhighlight>
{{out}}
<pre>
Line 732 ⟶ 1,745:
[21147; 25287; 30304; 36401; 43833; 52922; 64077; 77821; 94828; 115975]
</pre>
<
bell|>Seq.take 15|>Seq.iter(fun n->printf "%A " (List.head n));printfn ""
</syntaxhighlight>
{{out}}
<pre>
1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322
</pre>
<
printfn "%A" (Seq.head (Seq.item 49 bell))
</syntaxhighlight>
{{out}}
<pre>
Line 750 ⟶ 1,763:
===via Aitken's array===
{{works with|Factor|0.98}}
<
: next-row ( prev -- next )
Line 762 ⟶ 1,775:
"First 15 Bell numbers:\n%[%d, %]\n\n50th: %d\n\n" printf
"First 10 rows of the Bell triangle:" print
10 aitken [ "%[%d, %]\n" printf ] each</
{{out}}
<pre>
Line 785 ⟶ 1,798:
This solution makes use of a [https://en.wikipedia.org/wiki/Bell_number#Summation_formulas recurrence relation] involving binomial coefficients.
{{works with|Factor|0.98}}
<
: next-bell ( seq -- n )
Line 794 ⟶ 1,807:
50 bells [ 15 head ] [ last ] bi
"First 15 Bell numbers:\n%[%d, %]\n\n50th: %d\n" printf</
{{out}}
<pre>
Line 805 ⟶ 1,818:
This solution defines Bell numbers in terms of [https://en.wikipedia.org/wiki/Bell_number#Summation_formulas sums of Stirling numbers of the second kind].
{{works with|Factor|0.99 development release 2019-07-10}}
<
: bell ( m -- n )
Line 811 ⟶ 1,824:
50 [ bell ] { } map-integers [ 15 head ] [ last ] bi
"First 15 Bell numbers:\n%[%d, %]\n\n50th: %d\n" printf</
{{out}}
As above.
=={{header|
FB does not yet offer native support for Big Ints.
<syntaxhighlight lang="futurebasic">
local fn BellNumbers( limit as long )
long j, n = 1
mda(0) = 1
printf @"%2llu. %19llu", n, mda_integer(0)
while ( n < limit )
mda(n) = mda(0)
for j = n to 1 step -1
mda(j - 1) = mda_integer(j - 1) + mda_integer(j)
next
n++
printf @"%2llu. %19llu", n, mda_integer(0)
wend
end fn
fn BellNumbers( 25 )
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
1. 1
2. 2
3. 5
4. 15
5. 52
6. 203
7. 877
8.
9.
10. 115975
11. 678570
12. 4213597
13. 27644437
14. 190899322
15. 1382958545
16. 10480142147
17. 82864869804
18. 682076806159
19. 5832742205057
20. 51724158235372
21. 474869816156751
22. 4506715738447323
23. 44152005855084346
24. 445958869294805289
25. 4638590332229999353
</pre>
=={{header|Go}}==
<
import (
Line 876 ⟶ 1,916:
fmt.Println(bt[i])
}
}</
{{out}}
Line 913 ⟶ 1,953:
=={{header|Groovy}}==
{{trans|Java}}
<
private static class BellTriangle {
private List<Integer> arr
Line 970 ⟶ 2,010:
}
}
}</
{{out}}
<pre>First fifteen Bell numbers:
Line 1,001 ⟶ 2,041:
=={{header|Haskell}}==
<
bellTri =
let f xs = (last xs, xs)
in map snd (iterate (f
bell :: [Integer]
bell = map head bellTri
main :: IO ()
main = do
putStrLn "First 10 rows of Bell's Triangle:"
mapM_ print (take 10 bellTri)
putStrLn "
mapM_ print (take 15 bell)
putStrLn "
print (bell !! 49)</syntaxhighlight>
{{out}}
<pre>First 10 rows of Bell's Triangle:
[1]
[1,2]
Line 1,030 ⟶ 2,070:
[4140,5017,6097,7432,9089,11155,13744,17007,21147]
[21147,25287,30304,36401,43833,52922,64077,77821,94828,115975]
First 15 Bell numbers:
1
1
Line 1,046 ⟶ 2,087:
27644437
190899322
50th Bell number:
10726137154573358400342215518590002633917247281</pre>
And, of course, in terms of ''Control.Arrow'' or ''Control.Applicative'', the triangle function could also be written as:
<
bellTri :: [[Integer]]
bellTri = map snd (iterate ((last &&& id) . uncurry (scanl (+))) (1,[1]))</
or:
<
bellTri :: [[Integer]]
bellTri = map snd (iterate ((liftA2 (,) last id) . uncurry (scanl (+))) (1,[1]))</
or, as an applicative without the need for an import:
<
bellTri = map snd (iterate (((,) . last <*> id) . uncurry (scanl (+))) (1, [1]))</
=={{header|J}}==
<syntaxhighlight lang="text">
bell=: ([: +/\ (,~ {:))&.>@:{:
Line 1,090 ⟶ 2,131:
{:>bell^:49<1x
185724268771078270438257767181908917499221852770
</syntaxhighlight>
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.util.List;
Line 1,154 ⟶ 2,195:
}
}
}</
{{out}}
<pre>First fifteen Bell numbers:
Line 1,182 ⟶ 2,223:
4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147
21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975</pre>
=={{header|jq}}==
{{trans|Julia}}
<syntaxhighlight lang="jq"># nth Bell number
def bell:
. as $n
| if $n < 0 then "non-negative integer expected"
elif $n < 2 then 1
else
reduce range(1; $n) as $i ([1];
reduce range(1; $i) as $j (.;
.[$i - $j] as $x
| .[$i - $j - 1] += $x )
| .[$i] = .[0] + .[$i - 1] )
| .[$n - 1]
end;
# The task
range(1;51) | bell</syntaxhighlight>
{{out}}
For displaying the results, we will first use gojq, the Go implementation of jq, as it supports unbounded-precision integer arithmetic.
<pre>1
2
5
15
...
37450059502461511196505342096431510120174682
628919796303118415420210454071849537746015761
10726137154573358400342215518590002633917247281
185724268771078270438257767181908917499221852770
</pre>
Using the C-based implementation of jq, the results become inexact from bell(23) onwards:
<pre>[1,1]
[2,2]
...
[21,474869816156751]
[22,4506715738447323]
# inexact
[23,44152005855084344]
[24,445958869294805300]
...
[49,1.0726137154573358e+46]
[50,1.8572426877107823e+47]
</pre>
=={{header|Julia}}==
Source: Combinatorics at https://github.com/JuliaMath/Combinatorics.jl/blob/master/src/numbers.jl
<
bellnum(n)
Compute the ``n``th Bell number.
Line 1,208 ⟶ 2,294:
for i in 1:50
println(bellnum(i))
end</syntaxhighlight>
<pre>
1
Line 1,265 ⟶ 2,351:
=={{header|Kotlin}}==
{{trans|C}}
<
private val arr: Array<Int>
Line 1,316 ⟶ 2,402:
println()
}
}</
{{out}}
<pre>First fifteen Bell numbers:
Line 1,344 ⟶ 2,430:
4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147
21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975</pre>
=={{header|Little Man Computer}}==
Demonstrates an algorithm that uses a 1-dimensional array and addition. The maximum integer on the LMC is 999, so the maximum Bell number found is B_7 = 877.
In order to handle arrays, a program for the LMC has to modify its own code. This practice is usually frowned on nowadays, but was standard on very early real-life computers, such as EDSAC.
<syntaxhighlight lang="little man computer">
// Little Man Computer, for Rosetta Code.
// Calculate Bell numbers, using a 1-dimensional array and addition.
//
// After the calculation of B_n (n > 0), the array contains n elements,
// of which B_n is the first. Example to show calculation of B_(n+1):
// After calc. of B_3 = 5, array holds: 5, 3, 2
// Extend array by copying B_3 to high end: 5, 3, 2, 5
// Replace 2 by 5 + 2 = 7: 5, 3, 7, 5
// Replace 3 by 7 + 3 = 10: 5, 10, 7, 5
// Replace first 5 by 10 + 5 = 15: 15, 10, 7, 5
// First element of array is now B_4 = 15.
// Initialize; B_0 := 1
LDA c1
STA Bell
LDA c0
STA index
BRA print // skip increment of index
// Increment index of Bell number
inc_ix LDA index
ADD c1
STA index
// Here acc = index; print index and Bell number
print OUT
LDA colon
OTC // non-standard instruction; cosmetic only
LDA Bell
OUT
LDA index
BRZ inc_ix // if index = 0, skip rest and loop back
SUB c7 // reached maximum index yet?
BRZ done // if so, jump to exit
// Manufacture some instructions
LDA lda_0
ADD index
STA lda_ix
SUB c200 // convert LDA to STA with same address
STA sta_ix
// Copy latest Bell number to end of array
lda_0 LDA Bell // load Bell number
sta_ix STA 0 // address was filled in above
// Manufacture more instructions
LDA lda_ix // load LDA instruction
loop SUB c401 // convert to ADD with address 1 less
STA add_ix_1
ADD c200 // convert to STA
STA sta_ix_1
// Execute instructions; zero addresses were filled in above
lda_ix LDA 0 // load element of array
add_ix_1 ADD 0 // add to element below
sta_ix_1 STA 0 // update element below
LDA sta_ix_1 // load previous STA instruction
SUB sta_Bell // does it refer to first element of array?
BRZ inc_ix // yes, loop to inc index and print
LDA lda_ix // no, repeat with addresses 1 less
SUB c1
STA lda_ix
BRA loop
// Here when done
done HLT
// Constants
colon DAT 58
c0 DAT 0
c1 DAT 1
c7 DAT 7 // maximum index
c200 DAT 200
c401 DAT 401
sta_Bell STA Bell // not executed; used for comparison
// Variables
index DAT
Bell DAT
// Rest of array goes here
// end
</syntaxhighlight>
{{out}}
<pre>
[formatted manually]
0:1
1:1
2:2
3:5
4:15
5:52
6:203
7:877
</pre>
=={{header|Lua}}==
<
-- db 6/11/2020 (to replace missing original)
Line 1,374 ⟶ 2,552:
for i = 1, 10 do
print("[ " .. table.concat(tri[i],", ") .. " ]")
end</
{{out}}
<pre>First 15 and 25th Bell numbers:
Line 1,408 ⟶ 2,586:
=={{header|Maple}}==
<syntaxhighlight lang="maple">bell1:=proc(n)
option remember;
add(binomial(n-1,k)*bell1(k),k=0..n-1)
Line 1,424 ⟶ 2,601:
# [1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570,
# 4213597, 27644437, 190899322, 1382958545, 10480142147,
# 82864869804, 682076806159, 5832742205057, 51724158235372]</
=={{header|Mathematica}} / {{header|Wolfram Language}}==
'''Function definition:'''
<syntaxhighlight lang="mathematica">
BellTriangle[n_Integer?Positive] := NestList[Accumulate[# /. {a___, b_} :> {b, a, b}] &, {1}, n - 1];
BellNumber[n_Integer] := BellTriangle[n][[n, 1]];
</syntaxhighlight>
'''Output:'''
<syntaxhighlight lang="mathematica">
In[51]:= Array[BellNumber, 25]
Line 1,449 ⟶ 2,626:
6097, 7432, 9089, 11155, 13744, 17007, 21147}, {21147, 25287, 30304,
36401, 43833, 52922, 64077, 77821, 94828, 115975}}
</syntaxhighlight>
=={{header|Maxima}}==
It exists in Maxima the belln built-in function.
Below is another way
<syntaxhighlight lang="maxima">
/* Subfactorial numbers */
subfactorial(n):=block(
subf[0]:1,
subf[n]:n*subf[n-1]+(-1)^n,
subf[n])$
/* Bell numbers implementation */
my_bell(n):=if n=0 then 1 else block(
makelist((1/((n-1)!))*subfactorial(j)*binomial(n-1,j)*(n-j)^(n-1),j,0,n-1),
apply("+",%%))$
/* First 50 */
block(
makelist(my_bell(u),u,0,49),
table_form(%%));
</syntaxhighlight>
{{out}}
<pre>
matrix(
[1],
[1],
[2],
[5],
[15],
[52],
[203],
[877],
[4140],
[21147],
[115975],
[678570],
[4213597],
[27644437],
[190899322],
[1382958545],
[10480142147],
[82864869804],
[682076806159],
[5832742205057],
[51724158235372],
[474869816156751],
[4506715738447323],
[44152005855084346],
[445958869294805289],
[4638590332229999353],
[49631246523618756274],
[545717047936059989389],
[6160539404599934652455],
[71339801938860275191172],
[846749014511809332450147],
[10293358946226376485095653],
[128064670049908713818925644],
[1629595892846007606764728147],
[21195039388640360462388656799],
[281600203019560266563340426570],
[3819714729894818339975525681317],
[52868366208550447901945575624941],
[746289892095625330523099540639146],
[10738823330774692832768857986425209],
[157450588391204931289324344702531067],
[2351152507740617628200694077243788988],
[35742549198872617291353508656626642567],
[552950118797165484321714693280737767385],
[8701963427387055089023600531855797148876],
[139258505266263669602347053993654079693415],
[2265418219334494002928484444705392276158355],
[37450059502461511196505342096431510120174682],
[628919796303118415420210454071849537746015761],
[10726137154573358400342215518590002633917247281]
)
</pre>
=={{header|Modula-2}}==
{{trans|QuickBASIC}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<syntaxhighlight lang="modula2">
MODULE BellNumbers;
FROM STextIO IMPORT
WriteLn, WriteString;
FROM SWholeIO IMPORT
WriteInt;
CONST
MaxN = 14;
VAR
A: ARRAY [0 .. MaxN - 1] OF CARDINAL;
I, J, N: CARDINAL;
PROCEDURE DisplayRow(N, BellNum: CARDINAL);
BEGIN
WriteString("B(");
WriteInt(N, 2);
WriteString(") = ");
WriteInt(BellNum, 9);
WriteLn
END DisplayRow;
BEGIN
FOR I := 0 TO MaxN - 1 DO
A[I] := 0
END;
N := 0;
A[0] := 1;
DisplayRow(N, A[0]);
WHILE N < MaxN DO
A[N] := A[0];
FOR J := N TO 1 BY -1 DO
A[J - 1] := A[J - 1] + A[J]
END;
N := N + 1;
DisplayRow(N, A[0])
END
END BellNumbers.
</syntaxhighlight>
{{out}}
<pre>
B( 0) = 1
B( 1) = 1
B( 2) = 2
B( 3) = 5
B( 4) = 15
B( 5) = 52
B( 6) = 203
B( 7) = 877
B( 8) = 4140
B( 9) = 21147
B(10) = 115975
B(11) = 678570
B(12) = 4213597
B(13) = 27644437
B(14) = 190899322
</pre>
=={{header|Nim}}==
===Using Recurrence relation===
<
iterator b(): int =
Line 1,480 ⟶ 2,797:
inc i
if i > Limit:
break</
{{out}}
Line 1,512 ⟶ 2,829:
===Using Bell triangle===
<
## Iterator yielding the bell numbers.
var row = @[1]
Line 1,522 ⟶ 2,839:
for i in 1..newRow.high:
newRow[i] = newRow[i - 1] + row[i - 1]
row
yield row[^1] # The last value of the row is one step ahead of the first one.
Line 1,534 ⟶ 2,851:
for i in 1..newRow.high:
newRow[i] = newRow[i - 1] + row[i - 1]
row
yield row
Line 1,562 ⟶ 2,879:
echo line
if i == 10:
break</
{{out}}
Line 1,605 ⟶ 2,922:
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975</pre>
=={{header|PARIGP}}==
From the code at OEIS A000110,
<syntaxhighlight lang="text">
genit(maxx=50)={bell=List();
for(n=0,maxx,q=sum(k=0,n,stirling(n,k,2));
listput(bell,q));bell}
END</syntaxhighlight>
'''Output:'''
<nowiki>List([1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570, 4213597, 27644437, 190899322, 1382958545, 10480142147, 82864869804, 682076806159, 5832742205057, 51724158235372, 474869816156751, 4506715738447323, 44152005855084346, 445958869294805289, 4638590332229999353, 49631246523618756274, 545717047936059989389, 6160539404599934652455, 71339801938860275191172, 846749014511809332450147, 10293358946226376485095653, 128064670049908713818925644, 1629595892846007606764728147, 21195039388640360462388656799, 281600203019560266563340426570, 3819714729894818339975525681317, 52868366208550447901945575624941, 746289892095625330523099540639146, 10738823330774692832768857986425209, 157450588391204931289324344702531067, 2351152507740617628200694077243788988, 35742549198872617291353508656626642567, 552950118797165484321714693280737767385, 8701963427387055089023600531855797148876, 139258505266263669602347053993654079693415, 2265418219334494002928484444705392276158355, 37450059502461511196505342096431510120174682, 628919796303118415420210454071849537746015761, 10726137154573358400342215518590002633917247281, 185724268771078270438257767181908917499221852770])</nowiki>
=={{header|Pascal}}==
{{Works with|Free Pascal}}
Using bell's triangle. TIO.RUN up to 5000.See talk for more.
<syntaxhighlight lang="pascal">program BellNumbers;
{$Ifdef FPC}
{$optimization on,all}
{$ElseIf}
{Apptype console}
{$EndIf}
uses
sysutils,gmp;
var
T0 :TDateTime;
procedure BellNumbersUint64(OnlyBellNumbers:Boolean);
var
BList : array[0..24] of Uint64;
BellNum : Uint64;
BListLenght,i :nativeUInt;
begin
IF OnlyBellNUmbers then
Begin
writeln('Bell triangles ');
writeln(' 1 = 1');
end
else
Begin
writeln('Bell numbers');
writeln(' 1 = 1');
writeln(' 2 = 1');
end;
BList[0]:= 1;
BListLenght := 1;
BellNum := 1;
repeat
// For i := BListLenght downto 1 do BList[i] := BList[i-1]; or
move(Blist[0],Blist[1],BListLenght*SizeOf(Blist[0]));
BList[0] := BellNum;
For i := 1 to BListLenght do
Begin
BellNum += BList[i];
BList[i] := BellNum;
end;
// Output
IF OnlyBellNUmbers then
Begin
IF BListLenght<=9 then
Begin
write(BListLenght+1:3,' = ');
For i := 0 to BListLenght do
write(BList[i]:7);
writeln;
end
ELSE
BREAK;
end
else
writeln(BListLenght+2:3,' = ',BellNum);
inc(BListLenght);
until BListLenght >= 25;
writeln;
end;
procedure BellNumbersMPInteger;
const
MaxIndex = 5000;//must be > 0
var
//MPInteger as alternative to mpz_t -> selfcleaning
BList : array[0..MaxIndex] of MPInteger;
BellNum : MPInteger;
BListLenght,i :nativeUInt;
BellNumStr : AnsiString;
Begin
BellNumStr := '';
z_init(BellNum);
z_ui_pow_ui(BellNum,10,32767);
BListLenght := z_size(BellNum);
writeln('init length ',BListLenght);
For i := 0 to MaxIndex do
Begin
// z_init2_set(BList[i],BListLenght);
z_add_ui( BList[i],i);
end;
writeln('init length ',z_size(BList[0]));
T0 := now;
BListLenght := 1;
z_set_ui(BList[0],1);
z_set_ui(BellNum,1);
repeat
//Move does not fit moving interfaces // call fpc_intf_assign
For i := BListLenght downto 1 do BList[i] := BList[i-1];
z_set(BList[0],BellNum);
For i := 1 to BListLenght do
Begin
BellNum := z_add(BellNum,BList[i]);
z_set(BList[i],BellNum);
end;
inc(BListLenght);
if (BListLenght+1) MOD 100 = 0 then
Begin
BellNumStr:= z_get_str(10,BellNum);
//z_sizeinbase (BellNum, 10) is not exact :-(
write('Bell(',(IntToStr(BListLenght)):6,') has ',
(IntToStr(Length(BellNumStr))):6,' decimal digits');
writeln(FormatDateTime(' NN:SS.ZZZ',now-T0),'s');
end;
until BListLenght>=MaxIndex;
BellNumStr:= z_get_str(10,BellNum);
writeln(BListLenght:6,'.th ',Length(BellNumStr):8);
//clean up ;-)
BellNumStr := '';
z_clear(BellNum);
For i := MaxIndex downto 0 do
z_clear(BList[i]);
end;
BEGIN
BellNumbersUint64(True);BellNumbersUint64(False);
BellNumbersMPInteger;
END.</syntaxhighlight>
{{out}}
<pre style="height:180px">
TIO.RUN
Real time: 22.818 s User time: 22.283 s Sys. time: 0.109 s CPU share: 98.13 %
Bell triangles
1 = 1
2 = 1 2
3 = 2 3 5
4 = 5 7 10 15
5 = 15 20 27 37 52
6 = 52 67 87 114 151 203
7 = 203 255 322 409 523 674 877
8 = 877 1080 1335 1657 2066 2589 3263 4140
9 = 4140 5017 6097 7432 9089 11155 13744 17007 21147
10 = 21147 25287 30304 36401 43833 52922 64077 77821 94828 115975
Bell numbers
1 = 1
2 = 1
3 = 2
4 = 5
5 = 15
6 = 52
7 = 203
8 = 877
9 = 4140
10 = 21147
11 = 115975
12 = 678570
13 = 4213597
14 = 27644437
15 = 190899322
16 = 1382958545
17 = 10480142147
18 = 82864869804
19 = 682076806159
20 = 5832742205057
21 = 51724158235372
22 = 474869816156751
23 = 4506715738447323
24 = 44152005855084346
25 = 445958869294805289
26 = 4638590332229999353
init length 1701
init length 0
Bell( 99) has 115 decimal digits 00:00.001s
Bell( 199) has 275 decimal digits 00:00.005s
Bell( 299) has 453 decimal digits 00:00.013s
Bell( 399) has 643 decimal digits 00:00.022s
Bell( 499) has 842 decimal digits 00:00.035s
Bell( 599) has 1048 decimal digits 00:00.051s
Bell( 699) has 1260 decimal digits 00:00.071s
Bell( 799) has 1478 decimal digits 00:00.098s
Bell( 899) has 1700 decimal digits 00:00.128s
Bell( 999) has 1926 decimal digits 00:00.167s
Bell( 1099) has 2155 decimal digits 00:00.208s
Bell( 1199) has 2388 decimal digits 00:00.256s
Bell( 1299) has 2625 decimal digits 00:00.310s
Bell( 1399) has 2864 decimal digits 00:00.366s
Bell( 1499) has 3105 decimal digits 00:00.440s
Bell( 1599) has 3349 decimal digits 00:00.517s
Bell( 1699) has 3595 decimal digits 00:00.608s
Bell( 1799) has 3844 decimal digits 00:00.711s
Bell( 1899) has 4095 decimal digits 00:00.808s
Bell( 1999) has 4347 decimal digits 00:00.959s
Bell( 2099) has 4601 decimal digits 00:01.189s
Bell( 2199) has 4858 decimal digits 00:01.373s
Bell( 2299) has 5115 decimal digits 00:01.560s
Bell( 2399) has 5375 decimal digits 00:01.816s
Bell( 2499) has 5636 decimal digits 00:02.065s
Bell( 2599) has 5898 decimal digits 00:02.304s
Bell( 2699) has 6162 decimal digits 00:02.669s
Bell( 2799) has 6428 decimal digits 00:02.962s
Bell( 2899) has 6694 decimal digits 00:03.366s
Bell( 2999) has 6962 decimal digits 00:03.720s
Bell( 3099) has 7231 decimal digits 00:04.145s
Bell( 3199) has 7502 decimal digits 00:04.554s
Bell( 3299) has 7773 decimal digits 00:05.198s
Bell( 3399) has 8046 decimal digits 00:05.657s
Bell( 3499) has 8320 decimal digits 00:06.270s
Bell( 3599) has 8595 decimal digits 00:06.804s
Bell( 3699) has 8871 decimal digits 00:07.475s
Bell( 3799) has 9148 decimal digits 00:08.189s
Bell( 3899) has 9426 decimal digits 00:08.773s
Bell( 3999) has 9704 decimal digits 00:09.563s
Bell( 4099) has 9984 decimal digits 00:10.411s
Bell( 4199) has 10265 decimal digits 00:11.301s
Bell( 4299) has 10547 decimal digits 00:12.230s
Bell( 4399) has 10829 decimal digits 00:13.415s
Bell( 4499) has 11112 decimal digits 00:14.830s
Bell( 4599) has 11397 decimal digits 00:16.630s
Bell( 4699) has 11682 decimal digits 00:18.210s
Bell( 4799) has 11968 decimal digits 00:19.964s
Bell( 4899) has 12254 decimal digits 00:21.332s
Bell( 4999) has 12542 decimal digits 00:22.445s
5000.th 12544
</pre>
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use feature 'say';
Line 1,628 ⟶ 3,178:
say "\nFirst ten rows of Aitken's array:";
printf '%-7d'x@{$Aitkens[$_]}."\n", @{$Aitkens[$_]} for 0..9;</
{{out}}
<pre>First fifteen and fiftieth Bell numbers:
Line 1,664 ⟶ 3,214:
{{libheader|Phix/mpfr}}
Started out as a translation of Go, but the main routine has now been completely replaced.
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">function</span> <span style="color: #000000;">bellTriangle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- nb: returns strings to simplify output</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">sz</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"1"</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tri</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</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>
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">tri</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tri</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">sz</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">tri</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tri</span><span style="color: #0000FF;">[$],</span><span style="color: #000000;">sz</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">tri</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">bt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">bellTriangle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">50</span><span style="color: #0000FF;">)</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;">"First fifteen and fiftieth Bell numbers:\n%s\n50:%s\n\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bt</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">15</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">bt</span><span style="color: #0000FF;">[</span><span style="color: #000000;">50</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]})</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;">"The first ten rows of Bell's triangle:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</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;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bt</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,709 ⟶ 3,263:
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975
</pre>
=={{header|Picat}}==
'''First 18 Bell numbers and b(50).'''
(Port of the Sage solution at the OEIS A000110 page.)
<syntaxhighlight lang="picat">main =>
B50=b(50),
println(B50[1..18]),
println(b50=B50.last),
nl.
b(M) = R =>
A = new_array(M-1),
bind_vars(A,0),
A[1] := 1,
R = [1, 1],
foreach(N in 2..M-1)
A[N] := A[1],
foreach(K in N..-1..2)
A[K-1] := A[K-1] + A[K],
end,
R := R ++ [A[1]]
end.</syntaxhighlight>
{{out}}
<pre>[1,1,2,5,15,52,203,877,4140,21147,115975,678570,4213597,27644437,190899322,1382958545,10480142147,82864869804]
b50 = 10726137154573358400342215518590002633917247281</pre>
'''Bell's Triangle (and the 50th Bell number)'''
{{trans|D}}
<syntaxhighlight lang="picat">main =>
Tri = tri(50),
foreach(I in 1..10)
println(Tri[I].to_list)
end,
nl,
println(tri50=Tri.last.first),
nl.
% Adjustments for base-1.
tri(N) = Tri[2..N+1] =>
Tri = new_array(N+1),
foreach(I in 1..N+1)
Tri[I] := new_array(I-1),
bind_vars(Tri[I],0)
end,
Tri[2,1] := 1,
foreach(I in 3..N+1)
Tri[I,1] := Tri[I-1,I-2],
foreach(J in 2..I-1)
Tri[I,J] := Tri[I,J-1] + Tri[I-1,J-1]
end
end.</syntaxhighlight>
{{out}}
<pre>[1]
[1,2]
[2,3,5]
[5,7,10,15]
[15,20,27,37,52]
[52,67,87,114,151,203]
[203,255,322,409,523,674,877]
[877,1080,1335,1657,2066,2589,3263,4140]
[4140,5017,6097,7432,9089,11155,13744,17007,21147]
[21147,25287,30304,36401,43833,52922,64077,77821,94828,115975]
tri50 = 10726137154573358400342215518590002633917247281</pre>
{{trans|Prolog}}
<syntaxhighlight lang="picat">main :-
bell(49, Bell),
printf("First 15 Bell numbers:\n"),
print_bell_numbers(Bell, 15),
Number=Bell.last.first,
printf("\n50th Bell number: %w\n", Number),
printf("\nFirst 10 rows of Bell triangle:\n"),
print_bell_rows(Bell, 10).
bell(N, Bell):-
bell(N, Bell, [], _).
bell(0, [[1]|T], T, [1]):-!.
bell(N, Bell, B, Row):-
N1 is N - 1,
bell(N1, Bell, [Row|B], Last),
next_row(Row, Last).
next_row([Last|Bell], Bell1):-
Last=last(Bell1),
next_row1(Last, Bell, Bell1).
next_row1(_, [], []):-!.
next_row1(X, [Y|Rest], [B|Bell]):-
Y is X + B,
next_row1(Y, Rest, Bell).
print_bell_numbers(_, 0):-!.
print_bell_numbers([[Number|_]|Bell], N):-
printf("%w\n", Number),
N1 is N - 1,
print_bell_numbers(Bell, N1).
print_bell_rows(_, 0):-!.
print_bell_rows([Row|Rows], N):-
print_bell_row(Row),
N1 is N - 1,
print_bell_rows(Rows, N1).
print_bell_row([Number]):-
!,
printf("%w\n", Number).
print_bell_row([Number|Numbers]):-
printf("%w ", Number),
print_bell_row(Numbers).</syntaxhighlight>
{{out}}
<pre>First 15 Bell numbers:
1
1
2
5
15
52
203
877
4140
21147
115975
678570
4213597
27644437
190899322
50th Bell number: 10726137154573358400342215518590002633917247281
First 10 rows of Bell triangle:
1
1 2
2 3 5
5 7 10 15
15 20 27 37 52
52 67 87 114 151 203
203 255 322 409 523 674 877
877 1080 1335 1657 2066 2589 3263 4140
4140 5017 6097 7432 9089 11155 13744 17007 21147
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975</pre>
===Constraint modelling: construct (and count) the sets===
This model creates the underlying structure of the sets, and for N=1..4 it also converts and show the proper sets.
What the constraint model really creates is the following for N=3:
[[1,1,1],[1,1,2],[1,2,1],[1,2,2],[1,2,3]]
where the i'th value in a (sub) list indicates which set it belongs to.
E.g. the list
[1,2,1]
indicates that both 1 and 3 belongs to the first set, and 2 belongs to the second set, i.e.
{{1,3},{2}, {}}
and after the empty lists are removed:
{{1,3},{2}}
The full set is converted to
{{{1,2,3}},{{1,2},{3}},{{1,3},{2}},{{1},{2,3}},{{1},{2},{3}}}
The symmetry constraint <code>value_precede_chain/2</code> ensures that a value N+1 is not placed in the list (X) before all the values 1..N has been placed ("seen") in the list. This handles the symmetry that the two sets {1,2} and {2,1} are to be considered the same.
<syntaxhighlight lang="picat">import cp.
main =>
member(N,1..10),
X = new_list(N),
X :: 1..N,
value_precede_chain(1..N,X),
solve_all($[ff,split],X)=All,
println(N=All.len),
if N <= 4 then
% convert to sets
Set = {},
foreach(Y in All)
L = new_array(N),
bind_vars(L,{}),
foreach(I in 1..N)
L[Y[I]] := L[Y[I]] ++ {I}
end,
Set := Set ++ { {E : E in L, E != {}} }
end,
println(Set)
end,
nl,
fail,
nl.
%
% Ensure that a value N+1 is placed in the list X not before
% all the value 1..N are placed in the list.
%
value_precede_chain(C, X) =>
foreach(I in 2..C.length)
value_precede(C[I-1], C[I], X)
end.
value_precede(S,T,X) =>
XLen = X.length,
B = new_list(XLen+1),
B :: 0..1,
foreach(I in 1..XLen)
Xis #= (X[I] #= S),
(Xis #=> (B[I+1] #= 1))
#/\ ((#~ Xis #= 1) #=> (B[I] #= B[I+1]))
#/\ ((#~ B[I] #= 1) #=> (X[I] #!= T))
end,
B[1] #= 0.</syntaxhighlight>
{{out}}
<pre>1 = 1
{{{1}}}
2 = 2
{{{1,2}},{{1},{2}}}
3 = 5
{{{1,2,3}},{{1,2},{3}},{{1,3},{2}},{{1},{2,3}},{{1},{2},{3}}}
4 = 15
{{{1,2,3,4}},{{1,2,3},{4}},{{1,2,4},{3}},{{1,2},{3,4}},{{1,2},{3},{4}},{{1,3,4},{2}},{{1,3},{2,4}},{{1,3},{2},{4}},{{1,4},{2,3}},{{1},{2,3,4}},{{1},{2,3},{4}},{{1,4},{2},{3}},{{1},{2,4},{3}},{{1},{2},{3,4}},{{1},{2},{3},{4}}}
5 = 52
6 = 203
7 = 877
8 = 4140
9 = 21147
10 = 115975</pre>
=={{header|PicoLisp}}==
<
(make
(setq L (link (list 1)))
Line 1,728 ⟶ 3,516:
(prinl "First ten rows:")
(for N 10
(println (get L N)) )</
{{out}}
<pre>
Line 1,763 ⟶ 3,551:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
bell(N, Bell, [], _).
Line 1,807 ⟶ 3,595:
writef('\n50th Bell number: %w\n', [Number]),
writef('\nFirst 10 rows of Bell triangle:\n'),
print_bell_rows(Bell, 10).</
{{out}}
Line 1,847 ⟶ 3,635:
{{trans|D}}
{{Works with|Python|2.7}}
<
tri = [None] * n
for i in xrange(n):
Line 1,869 ⟶ 3,657:
print bt[i]
main()</
{{out}}
<pre>First fifteen and fiftieth Bell numbers:
Line 1,904 ⟶ 3,692:
{{Trans|Haskell}}
{{Works with|Python|3.7}}
<
from itertools import accumulate, chain, islice
Line 2,101 ⟶ 3,889:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>First fifteen Bell numbers:
Line 2,134 ⟶ 3,922:
9 -> [4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147]
10 -> [21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975]</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ ' [ [ 1 ] ] ' [ 1 ]
rot 1 - times
[ dup -1 peek nested
swap witheach
[ over -1 peek + join ]
tuck nested join swap ]
drop ] is bell's-triangle ( n --> [ )
[ bell's-triangle [] swap
witheach [ 0 peek join ] ] is bell-numbers ( n --> [ )
say "First fifteen Bell numbers:" cr
15 bell-numbers echo
cr cr
say "Fiftieth Bell number:" cr
50 bell-numbers -1 peek echo
cr cr
say "First ten rows of Bell's triangle:" cr
10 bell's-triangle witheach [ echo cr ]</syntaxhighlight>
{{out}}
<pre>First fifteen Bell numbers:
[ 1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322 ]
Fiftieth Bell number:
10726137154573358400342215518590002633917247281
First ten rows of Bell's triangle:
[ 1 ]
[ 1 2 ]
[ 2 3 5 ]
[ 5 7 10 15 ]
[ 15 20 27 37 52 ]
[ 52 67 87 114 151 203 ]
[ 203 255 322 409 523 674 877 ]
[ 877 1080 1335 1657 2066 2589 3263 4140 ]
[ 4140 5017 6097 7432 9089 11155 13744 17007 21147 ]
[ 21147 25287 30304 36401 43833 52922 64077 77821 94828 115975 ]
</pre>
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
(define (build-bell-row previous-row)
(define seed (last previous-row))
(reverse
(let-values (((reversed _) (for/fold ((acc (list seed)) (prev seed))
((pprev previous-row))
(let ((n (+ prev pprev))) (values (cons n acc) n)))))
reversed)))
(define reverse-bell-triangle
(let ((memo (make-hash '((0 . ((1)))))))
(λ (rows) (hash-ref! memo
rows
(λ ()
(let ((prev (reverse-bell-triangle (sub1 rows))))
(cons (build-bell-row (car prev)) prev)))))))
(define bell-triangle (compose reverse reverse-bell-triangle))
(define bell-number (compose caar reverse-bell-triangle))
(module+ main
(map bell-number (range 15))
(bell-number 50)
(bell-triangle 10))</syntaxhighlight>
{{out}}
<pre>'(1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322)
185724268771078270438257767181908917499221852770
'((1)
(1 2)
(2 3 5)
(5 7 10 15)
(15 20 27 37 52)
(52 67 87 114 151 203)
(203 255 322 409 523 674 877)
(877 1080 1335 1657 2066 2589 3263 4140)
(4140 5017 6097 7432 9089 11155 13744 17007 21147)
(21147 25287 30304 36401 43833 52922 64077 77821 94828 115975)
(115975 137122 162409 192713 229114 272947 325869 389946 467767 562595 678570))
</pre>
=={{header|Raku}}==
Line 2,140 ⟶ 4,017:
{{works with|Rakudo|2019.03}}
<syntaxhighlight lang="raku"
my @c = @b.tail;
@c.push: @b[$_] + @c[$_] for ^@b;
Line 2,152 ⟶ 4,029:
say "\nFirst ten rows of Aitken's array:";
.say for @Aitkens-array[^10];</
{{out}}
<pre>First fifteen and fiftieth Bell numbers:
Line 2,187 ⟶ 4,064:
{{works with|Rakudo|2019.03}}
<syntaxhighlight lang="raku"
my @bell = 1, -> *@s { [+] @s »*« @s.keys.map: { binomial(@s-1, $_) } } … *;
.say for @bell[^15], @bell[50 - 1];</
{{out}}
<pre>(1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322)
Line 2,199 ⟶ 4,076:
{{works with|Rakudo|2019.03}}
<syntaxhighlight lang="raku"
(1,),
{ (0, |@^last) »+« (|(@^last »*« @^last.keys), 0) } … *
Line 2,205 ⟶ 4,082:
my @bell = @Stirling_numbers_of_the_second_kind.map: *.sum;
.say for @bell.head(15), @bell[50 - 1];</
{{out}}
<pre>(1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322)
Line 2,219 ⟶ 4,096:
Also, see this task's [https://rosettacode.org/wiki/Talk:Bell_numbers#size_of_Bell_numbers ''discussion''] to view how the sizes of Bell numbers increase in relation to its index.
<
parse arg LO HI . /*obtain optional arguments from the CL*/
if LO=='' & HI=="" then do; LO=0; HI=14; end /*Not specified? Then use the default.*/
Line 2,242 ⟶ 4,119:
/*──────────────────────────────────────────────────────────────────────────────────────*/
fact: procedure expose !.; parse arg x; if !.x\==. then return !.x; != 1
do f=2 for x-1; != ! * f; end; !.x= !; return !</
{{out|output|text= when using the internal default inputs of: <tt> 0 14 </tt>}}
<pre>
Line 2,265 ⟶ 4,142:
Bell(49) = 10,726,137,154,573,358,400,342,215,518,590,002,633,917,247,281
</pre>
=={{header|RPL}}==
{{works with|HP|48}}
≪ → n
≪ { 1 }
'''WHILE''' 'n' DECR '''REPEAT'''
DUP DUP SIZE GET 1 →LIST
1 3 PICK SIZE '''FOR''' j
OVER j GET OVER j GET + +
'''NEXT''' SWAP DROP
'''END''' HEAD
≫ ≫ ‘<span style="color:blue">BELL</span>’ STO
'''Variant with a better use of the stack'''
Slightly faster then, although more wordy:
≪ → n
≪ { 1 } 1
'''WHILE''' 'n' DECR '''REPEAT'''
DUP 1 →LIST
1 4 PICK SIZE '''FOR''' j
3 PICK j GET ROT + SWAP OVER +
'''NEXT''' ROT DROP SWAP
'''END''' DROP HEAD
≫ ≫ ‘<span style="color:blue">BELL</span>’ STO
≪ {} 1 15 '''FOR''' n n <span style="color:blue">BELL</span> + '''NEXT''' ≫ EVAL
{{out}}
<pre>
1: { 1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322 }
</pre>
It makes no sense to display 10 rows of the Bell triangle on a screen limited to 22 characters and 7 lines in the best case.
=={{header|Ruby}}==
{{trans|D}}
<
tri = Array.new(n)
for i in 0 .. n - 1 do
Line 2,301 ⟶ 4,210:
end
main()</
{{out}}
<pre>First fifteen and fiftieth Bell numbers:
Line 2,336 ⟶ 4,245:
{{trans|D}}
{{libheader|num|0.2}}
<
fn main() {
Line 2,367 ⟶ 4,276:
tri
}
</syntaxhighlight>
{{out}}
Line 2,391 ⟶ 4,300:
=={{header|Scala}}==
{{trans|Java}}
<
object BellNumbers {
Line 2,450 ⟶ 4,359:
}
}
}</
{{out}}
<pre>First fifteen Bell numbers:
Line 2,478 ⟶ 4,387:
4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147
21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975</pre>
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<syntaxhighlight lang="scheme">; Given the remainder of the previous row and the final cons of the current row,
; extend (in situ) the current row to be a complete row of the Bell triangle.
; Return the final value in the extended row (for use in computing the following row).
(define bell-triangle-row-extend
(lambda (prevrest thisend)
(cond
((null? prevrest)
(car thisend))
(else
(set-cdr! thisend (list (+ (car prevrest) (car thisend))))
(bell-triangle-row-extend (cdr prevrest) (cdr thisend))))))
; Return the Bell triangle of rows 0 through N (as a list of lists).
(define bell-triangle
(lambda (n)
(let* ((tri (list (list 1)))
(triend tri)
(rowendval (caar tri)))
(do ((index 0 (1+ index)))
((>= index n) tri)
(let ((nextrow (list rowendval)))
(set! rowendval (bell-triangle-row-extend (car triend) nextrow))
(set-cdr! triend (list nextrow))
(set! triend (cdr triend)))))))
; Print out the Bell numbers 0 through 19 and 49 thgough 51.
; (The Bell numbers are the first number on each row of the Bell triangle.)
(printf "~%The Bell numbers:~%")
(let loop ((triangle (bell-triangle 51)) (count 0))
(when (pair? triangle)
(when (or (<= count 19) (>= count 49))
(printf " ~2d: ~:d~%" count (caar triangle)))
(loop (cdr triangle) (1+ count))))
; Print out the Bell triangle of 10 rows.
(printf "~%First 10 rows of the Bell triangle:~%")
(let rowloop ((triangle (bell-triangle 9)))
(when (pair? triangle)
(let eleloop ((rowlist (car triangle)))
(when (pair? rowlist)
(printf " ~7:d" (car rowlist))
(eleloop (cdr rowlist))))
(newline)
(rowloop (cdr triangle))))</syntaxhighlight>
{{out}}
<pre>The Bell numbers:
0: 1
1: 1
2: 2
3: 5
4: 15
5: 52
6: 203
7: 877
8: 4,140
9: 21,147
10: 115,975
11: 678,570
12: 4,213,597
13: 27,644,437
14: 190,899,322
15: 1,382,958,545
16: 10,480,142,147
17: 82,864,869,804
18: 682,076,806,159
19: 5,832,742,205,057
49: 10,726,137,154,573,358,400,342,215,518,590,002,633,917,247,281
50: 185,724,268,771,078,270,438,257,767,181,908,917,499,221,852,770
51: 3,263,983,870,004,111,524,856,951,830,191,582,524,419,255,819,477
First 10 rows of the Bell triangle:
1
1 2
2 3 5
5 7 10 15
15 20 27 37 52
52 67 87 114 151 203
203 255 322 409 523 674 877
877 1,080 1,335 1,657 2,066 2,589 3,263 4,140
4,140 5,017 6,097 7,432 9,089 11,155 13,744 17,007 21,147
21,147 25,287 30,304 36,401 43,833 52,922 64,077 77,821 94,828 115,975</pre>
=={{header|Sidef}}==
Built-in:
<
Formula as a sum of Stirling numbers of the second kind:
<
Via Aitken's array (optimized for space):
<
var acc = []
Line 2,503 ⟶ 4,499:
var B = bell_numbers(50)
say "The first 15 Bell numbers: #{B.first(15).join(', ')}"
say "The fiftieth Bell number : #{B[50-1]}"</
{{out}}
<pre>
Line 2,511 ⟶ 4,507:
Aitken's array:
<
var A = [1]
Line 2,520 ⟶ 4,516:
}
aitken_array(10).each { .say }</
{{out}}
<pre>
Line 2,536 ⟶ 4,532:
Aitken's array (recursive definition):
<
func A(n, (0)) { A(n-1, n-1) }
func A(n, k) is cached { A(n, k-1) + A(n-1, k-1) }
Line 2,542 ⟶ 4,538:
for n in (^10) {
say (0..n -> map{|k| A(n, k) })
}</
(same output as above)
Line 2,550 ⟶ 4,546:
{{trans|Kotlin}}
<
@usableFromInline
var arr: [T]
Line 2,592 ⟶ 4,588:
print()
}</
{{out}}
Line 2,623 ⟶ 4,619:
21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975</pre>
=={{header|
{{trans|
<syntaxhighlight lang="v (vlang)">import math.big
fn bell_triangle(n int) [][]big.Integer {
mut tri := [][]big.Integer{len: n}
tri[i] = []big.Integer{len: i}
tri[1][0] = big.integer_from_u64(1)
for i in 2..n {
tri[i][0] = tri[i-1][i-2]
}
}
}
fn main() {
bt := bell_triangle(51)
println("First fifteen and fiftieth Bell numbers:")
for
println("${i:2}:
println("50: ${bt[50][0]}")
println("\nThe first ten rows of Bell's triangle:")
for i := 1; i <= 10; i++ {
}
}</syntaxhighlight>
{{out}}
<pre>
First fifteen and fiftieth Bell numbers:
1: 1
2: 1
Line 2,708 ⟶ 4,684:
[877, 1080, 1335, 1657, 2066, 2589, 3263, 4140]
[4140, 5017, 6097, 7432, 9089, 11155, 13744, 17007, 21147]
[21147, 25287, 30304, 36401, 43833, 52922, 64077, 77821, 94828, 115975
</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./big" for BigInt
var bellTriangle = Fn.new { |n|
var tri = List.filled(n,
for (i in 0...n)
tri[
for (j in 0...i) tri[i][j] = BigInt.zero
}
tri[1][0] = BigInt.one
for (i in 2...n) {
tri[i][0] = tri[i-1][i-2]
Line 2,729 ⟶ 4,709:
}
var bt = bellTriangle.call(
System.print("First fifteen and fiftieth Bell numbers:")
for (i in 1..15)
Fmt.print("$2d: $,i", 50, bt[50][0])
System.print("\nThe first ten rows of Bell's triangle:")
for (i in 1..10)
{{out}}
<pre>
First fifteen and fiftieth Bell numbers:
1: 1
2: 1
Line 2,746 ⟶ 4,727:
7: 203
8: 877
9:
10:
11:
12:
13:
14:
15:
50: 10,726,137,154,573,358,400,342,215,518,590,002,633,917,247,281
The first ten rows of Bell's triangle:
1
1 2
4,140 5,017 6,097 7,432 9,089 11,155 13,744 17,007 21,147
21,147 25,287 30,304 36,401 43,833 52,922 64,077 77,821 94,828 115,975
</pre>
=={{header|XPL0}}==
{{trans|Delphi}}
32-bit integer are required to calculate the first 15 Bell numbers.
{{works with|EXPL-32}}
<syntaxhighlight lang="xpl0">
\Bell numbers
code CrLf=9, IntOut=11, Text=12;
define MaxN = 14;
integer A(MaxN), I, J, N;
begin
for I:= 0 to MaxN - 1 do A(I):= 0;
N:= 0; A(0):= 1;
Text(0, "B("); IntOut(0, N); Text(0, ") = "); IntOut(0, A(0)); CrLf(0);
while N < MaxN do
begin
A(N):= A(0);
for J:= N downto 1 do A(J - 1):= A(J - 1) + A(J);
N:= N + 1;
Text(0, "B("); IntOut(0, N); Text(0, ") = "); IntOut(0, A(0)); CrLf(0)
end;
end
</syntaxhighlight>
{{out}}
<pre>
B(0) = 1
B(1) = 1
B(2) = 2
B(3) = 5
B(4) = 15
B(5) = 52
B(6) = 203
B(7) = 877
B(8) = 4140
B(9) = 21147
B(10) = 115975
B(11) = 678570
B(12) = 4213597
B(13) = 27644437
B(14) = 190899322
</pre>
=={{header|zkl}}==
<
Walker.zero().tweak('wrap(row){
row.insert(0,row[-1]);
Line 2,774 ⟶ 4,798:
wantRow and row or row[-1]
}.fp(List(start))).push(start,start);
}</
<
bellTriangleW().walk(15).println();</
{{out}}
<pre>
Line 2,782 ⟶ 4,806:
L(1,1,2,5,15,52,203,877,4140,21147,115975,678570,4213597,27644437,190899322)
</pre>
<
bt:=bellTriangleW(1,True); do(11){ println(bt.next()) }</
{{out}}
<pre>
Line 2,800 ⟶ 4,824:
</pre>
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<
var [const] BI=Import("zklBigNum"); // libGMP
bellTriangleW(BI(1)).drop(50).value.println();</
{{out}}
<pre>
The fiftieth Bell number: 10726137154573358400342215518590002633917247281
</pre>
{{omit from|PL/0}}
{{omit from|Tiny BASIC}}
|