Jump to content

Bell numbers: Difference between revisions

Add Cowgol
(Added Arturo implementation)
(Add Cowgol)
Line 587:
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}}
<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;</lang>
 
{{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}}==
2,125

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.