Learned about the Pierce (or Peirce?) triangle from
http://www.research.att.com/~njas/sequences/A011971 and
http://mathworld.wolfram.com/BellTriangle.html
ptr =: ({:+[:+/\0&,)^:(]`1:)
NB. gives y-th row of P-triangle.
ptr i.4
1 0 0 0
1 2 0 0
2 3 5 0
5 7 10 15
Bell numbers:
{."1 ptr i.10
1 1 2 5 15 52 203 877 4140 21147
ts'bellN"0 i.120' NB. first call
10.202733 1509696
5 ts'bellN"0 i.120' NB. subsequent calls
0.23550545 13376
(bellN"0 i.120)-:{."1 ptr i.120x
1
5 ts'{."1 ptr i.120x'
0.00030992004 244864
which is (overall) 200000 times as efficient as the first call and 41 times
as subsequent calls.
ts'bellN1"0 i.120'
6.4776156 131776
(bellN1"0 i.120)-:{."1 ptr i.120x
1
which is (overall) 4300 times as efficient.
Hints:
- think arraywise
- avoid loops
- avoid low ranks
R.E. Boss
> -----Oorspronkelijk bericht-----
> Van: [EMAIL PROTECTED] [mailto:programming-
> [EMAIL PROTECTED] Namens Arie Groeneveld
> Verzonden: zaterdag 15 december 2007 17:01
> Aan: Programming forum
> Onderwerp: [Jprogramming] Bell numbers
>
> J4F
>
> Generating Bell numbers based on using
> the pierce Triangle.
>
> bell numbers: 1,1,2,5,15,52,203,877,4140,21147,115975
>
> I can come up with these two verbs
>
> NB. for n>21 I need to add ext. precision -- x:&]
> bellN=: [:{.([:+/\{:,x:&])^:(]`1:) M.
>
> NB. next pierce triangle row
> vp3hr=: 3 : 0
> v=.{:y
> for_i. i.#y do.
> v=. v,(i{v)+i{y
> end.
> )
>
> bellN1=: [:{.vp3hr^:(]`(x:@1:))
>
>
> bellN"0 i.10
> 1 1 2 5 15 52 203 877 4140 21147
>
> bellN1"0 i.10
> 1 1 2 5 15 52 203 877 4140 21147
>
> (bellN -: bellN1) 100
> 1
>
> timer 'bellN1 100'
> 0.086825
>
> timer 'bellN 100' NB. first call
> 0.305226
>
>
>
> Other possibilities?
>
>
> Regards
>
> =@@i
>
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm