Jump to content

Bell numbers: Difference between revisions

Added a Scheme implementation.
m (→‎{{header|PariGP}}: fix header)
(Added a Scheme implementation.)
Line 3,465:
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}}
<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))))</lang>
{{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}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.