The following code is an interpretation of the Haskell program (link can
be found in the RC task).
I used it to create a faster alternative (hopefully) for
NumOfParaffins n
NoParaff 15
1 1 1 1 2 3 5 9 18 35 75 159 355 802 1858
{: NofParaff 100
2156596319845084996862701478402986311496
It's especially faster when using ext. prec .numbers:
(NumOfParaffins-:!.0 NofParaff) 60
0
Here I need x: for NumOfParaffins
(NumOfParaffins-:!.0 NofParaff) 60x
1
ts 'NofParaff 60'
0.871579 486080
ts 'NumOfParaffins 60x'
5.13262 1.18509e6
Shoot, improve, whatever!
------------------ GEFREUBEL -----------------------
I did not focus to much on tacit expressions.
I replaced the select case constructions (I started with) with gerund
agenda constructions to squeeze the code a bit. AFAIR it had not much
impact on timings.
part3=: [: ;(<@([(],.(-+/"1))],.]+[:i.]-~1+<.@-:@-)"0 i.@>:@<.@%&3)
part4=: 3 :0
ij=. ; (,.]+i.@:(]-~1+[:<.3%~y-]))&.> i.1+<.y%4
ijk=. ; y 13 :'< y,"1 0 (<.-:x-s)(]+i.@>:@-) ({:y)>.(>.-:x)-s=.+/y'"0 1 ij
ijk,. y - +/"1 ijk
)
f0=: */@:{
f1=: 13 :'(*-:@(*>:))/y{~2{.x'
f2=: 13 :'(*-:@(*>:))~/y{~ _2{.x'
f3=: 13 :'3!2+y{~{.x'
NoR=: 3 :0
if. 0=y do. 1 return. end.
z=.1x
ppp=. part3&.> i.y-1
for_pp. ppp do.
t=.0
for_p. >pp do. t=.t+ p f0`f1`f2`f3 @. (#.@(2=/\[)) z end.
z=.z,t
end.
)
NB. (NoR n) bcpGenN n
bcpGenN=: [: , 0 ,.~ -:@(*>:)@({~i.) NB. +/@:(1 + [:
i.[{~<.@-:@])`0:@.(2|])"1 0
ff1=: 13 :'*/(y{~0 1{x), -:(*>:)y{~{:x'
ff2=: 13 :'*/(y{~0 3{x), -:(*>:)y{~2{x'
ff3=: 13 :'*/(y{~{.x) , y f3 ~}. x'
ff4=: 13 :'*/(y{~_2{.x), -:(*>:)y{~{.x'
ff5=: 13 :'*/ -:(*>:) y{~0 3{x'
ff6=: 13 :'*/(y{~{:x) , y f3 ~}: x'
ff7=: 13 :'4!3+y{~{.x'
ccpGenN=: 4 :0
if. 0=y do. 0 return. end.
z=.0x
ppp=. part4&.> i.y-1
for_pp. ppp do.
t=.0
if. (i.0 4)-:>pp do. else.
for_p. >pp do.
t=. t + p f0`ff1`ff2`ff3`ff4`ff5`ff6`ff7 @. (#.@(}.=}:)@[) x
end.
end.
z=.z,t
end.
)
NofParaff=: 13 :'y{. (NoR y)((ccpGenN +:)+ bcpGenN ) (2&|+<.@-:) y'
> tree=: 3 :0
> 'mx br n rdc sum l'=. y
> bs=. br+1
> k=. n{ra
> for_b. bs+i.4+1-bs do.
> if. mx <: s=. sum + n*t=. b-br do. return. end.
> c=. rdc *t !t+k-1
> if. s > 2*l do. unr=: s (c+{)`[`]} unr end.
> if. b=4 do. return. end.
> ra=: s (c+{)`[`]} ra
> for_m. (-i.)n-1 do. tree mx, b, m, c, s, l end.
> end.
> )
>
> bicenter=: 3 :0
> c=. -:(*>:) ra {~ -:y
> unr=: y (c+{)`[`]} unr
> )
>
> NumOfParaffins=: 3 :0
> if. 3>y do. y{.1 1 return. end.
> ra=: 1 1, 0$~ y-2
> unr=: 1 1, 0$~ y-2
> (]`bicenter@.(0=2&|) [ tree@:(y, 0, ], 1,1,]))"0 >: i.y-1
> unr
> )
>
>
--
Met vriendelijke groet,
@@i = Arie Groeneveld
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm