Bell numbers: Difference between revisions

m
Line 1,488:
25: 4638590332229999353</pre>
 
==={{Using Bell triangle}}===
<lang Nim>
iterator b(): int =
Line 1,526:
var i = 0
for n in b():
ec
echo fmt"{i:2d}: {n:>20d}"
inc i
if i > Limit:
break
 
echo "\nFirst ten rows of Bell triangle:"
i = 0
for row in bellTriangle():
inc i
var line = ""
for val in row:
line.addSep(" ", 0)
line.add(fmt"{val:6d}")
echo line
if i == 10:
break</lang>
 
{{out}}
<pre>Bell numbers from B0 to B25:
0: 1
1: 1
2: 2
3: 5
4: 15
5: 52
6: 203
7: 877
8: 4140
9: 21147
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
 
First ten 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>
 
{{out}}
<pre>
 
=={{header|Pascal}}==
{{works with| Free Pascal}}
Calculating in one row
<lang pascal>program BellNumbers;
uses
gmp;
 
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 = 49;//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 := '';
For i := 0 to MaxIndex do
z_init(BList[i]);
 
BListLenght := 1;
z_set_ui(BList[0],1);
z_init_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);
until BListLenght>=MaxIndex;
 
BellNumStr:= z_get_str(10,BellNum);
writeln(' 50.th Bell number',#13#10,BellNumStr);
 
//clean up ;-)
BellNumStr := '';
z_clear(BellNum);
For i := MaxIndex downto 0 do
z_clear(BList[i]);
end;
 
BEGIN
BellNumbersUint64(True);
BellNumbersUint64(False);
BellNumbersMPInteger;
END.</lang>
{{out}}
<pre "style=height: 120px">
 
Bell triangles
1 = 1
3 = 1 2
4 = 2 3 5
5 = 5 7 10 15
6 = 15 20 27 37 52
7 = 52 67 87 114 151 203
8 = 203 255 322 409 523 674 877
9 = 877 1080 1335 1657 2066 2589 3263 4140
10 = 4140 5017 6097 7432 9089 11155 13744 17007 21147
11 = 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
..
25 = 445958869294805289
26 = 4638590332229999353
 
50.th Bell number
10726137154573358400342215518590002633917247281
</pre>
 
=={{header|Perl}}==
Anonymous user