Bell numbers: Difference between revisions

10,878 bytes added ,  2 months ago
m
(→‎{{header|ASIC}}: Added a solution.)
m (→‎{{header|ALGOL W}}: fixed trans)
 
(18 intermediate revisions by 9 users not shown)
Line 296:
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}}==
Line 441 ⟶ 482:
 
=={{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}}
Line 537 ⟶ 621:
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}}===
Line 561 ⟶ 671:
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}}===
Line 1,345 ⟶ 1,607:
 
const
MAX_INDEXMAX_N = 25; // maximum index of Bell number within the limits of int64
var
n : integer; // index of Bell number
j : integer; // loop variable
a : array [0..MAX_INDEXMAX_N - 1] of int64; // working array to build up B_n
 
{ Subroutine to display that a[0] is the Bell number B_n }
Line 1,362 ⟶ 1,624:
a[0] := 1;
Display(); // some programmers would prefer Display;
while (n < MAX_INDEXMAX_N) do begin // and give begin a line to itself
a[n] := a[0];
for j := n downto 1 do inc( a[j - 1], a[j]);
Line 1,399 ⟶ 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}}==
Line 1,546 ⟶ 1,827:
{{out}}
As above.
 
=={{header|FutureBasic}}==
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. 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
</pre>
 
=={{header|Go}}==
Line 2,295 ⟶ 2,627:
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}}==
Line 2,367 ⟶ 2,839:
for i in 1..newRow.high:
newRow[i] = newRow[i - 1] + row[i - 1]
row.shallowCopy = move(newRow)
yield row[^1] # The last value of the row is one step ahead of the first one.
 
Line 2,379 ⟶ 2,851:
for i in 1..newRow.high:
newRow[i] = newRow[i - 1] + row[i - 1]
row.shallowCopy = move(newRow)
yield row
 
Line 2,449 ⟶ 2,921:
4140 5017 6097 7432 9089 11155 13744 17007 21147
21147 25287 30304 36401 43833 52922 64077 77821 94828 115975</pre>
 
=={{header|PARIGP}}==
From the code at OEIS A000110,
Line 3,669 ⟶ 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}}==
Line 4,185 ⟶ 4,690:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
var bellTriangle = Fn.new { |n|
Line 4,251 ⟶ 4,756:
\Bell numbers
code CrLf=9, IntOut=11, Text=12;
define MaxIndexMaxN = 14;
integer A(MaxIndexMaxN), I, J, N;
 
begin
for I:= 0 to MaxIndexMaxN - 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 < MaxIndexMaxN do
begin
A(N):= A(0);
3,044

edits