Bell numbers: Difference between revisions

→‎{{header|Factor}}: add two more methods
(→‎{{header|Factor}}: add two more methods)
Line 139:
 
=={{header|Factor}}==
===via Aitken's array===
{{works with|Factor|0.98}}
<lang factor>USING: formatting io kernel math math.matrices sequences vectors ;
 
: next-row ( prev -- next )
[ 1 1vector ]
[ dup last [ + ] accumulate swap suffix! ] if-empty ;
 
: aitken ( n -- seq )
V{ } clone swap [ next-row dup ] replicate nip ;
 
0 50 aitken col [ 15 head ] [ last ] bi
"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</lang>
{{out}}
<pre>
First 15 Bell numbers:
{ 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570, 4213597, 27644437, 190899322 }
 
50th: 10726137154573358400342215518590002633917247281
 
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>
===via recurrence relation===
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}}
<lang factor>USING: formatting kernel math math.combinatorics sequences ;
 
Line 157 ⟶ 193:
50th: 10726137154573358400342215518590002633917247281
</pre>
===via Stirling sums===
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}}
<lang factor>USING: formatting kernel math math.extras math.ranges sequences ;
 
: bell ( m -- n )
[ 1 ] [ dup [1,b] [ stirling ] with map-sum ] if-zero ;
 
50 [ bell ] { } map-integers [ 15 head ] [ last ] bi
"First 15 Bell numbers:\n%[%d, %]\n\n50th: %d\n" printf</lang>
{{out}}
As above.
 
=={{header|Go}}==
1,827

edits