Joey K Tuttle wrote:
> 
> At 13:43  -0500 2009/12/07, Richard Kubina wrote:
>>..well the  >:  to i.1000 is silly... a slight fix:
>>
>>  >./(#~ (= (|.&.":)"0)) ,*/~i.1000
>>
>>Sorry for double post, this is exciting (:
> 
> 
> I agree with Don that your use of &. is cute. Adding one more token ( 
> ~. ) to only work on unique values would speed up your algorithm by a 
> factor of 4 -
> 
>    >./(#~ (= (|.&.":)"0)) ~. ,*/~i.1000
> 
> A little different and more explicit route would be -
> 
>    maxpal =: 3 :
> '>./(*./zd=|.zd)#z[zd=.''0123456789''i.|:":,.z=.~.,*/~i.y'
> 
> which is considerably faster. I think it is easier to parse the 
> explicit verb than a generated tacit one:
> 
>    13 : '>./(*./zd=|.zd)#z[zd=.''0123456789''i.|:":,.z=.~.,*/~i.y'
> ([: ('0123456789' i. [: |: [: ": ,.) [: ~. [: , [: */~ i.) ([: >./ 
> ([: ([: *./ ] = |.) [) # ]) [: ~. [: , [: */~ i.
> 
> And the generated tacit one is a little slower than the explicit one, 
> the (normalized) time comparisons are:
> 
>   Time   Expression
>   ____   _______________________
>   1      >./(#~ (= (|.&.":)"0)) ,*/~i.1000
>   0.259  >./(#~ (= (|.&.":)"0)) ~. ,*/~i.1000
>   0.026  >./(*./zd=|.zd)#z[zd=.'0123456789'i.|:":,.z=.~.,*/~i.1000
> 
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> 
> 
The automatically generated verb that you give: 

maxpal1=:([: ('0123456789' i. [: |: [: ": ,.) [: ~. [: , [: */~ i.) ([: >./
([: ([: *./ ] = |.) [) # ]) [: ~. [: , [: */~ i.

is sub-optimal since it computes z twice.

If we transform it by hand, we get:

maxpal2=:'0123456789'&i.@:|:@:":@:,.@:~.@:,@:(*/~)@:i. (*./@:(= |.)@:[
>./@:# ]) ~.@:,@:(*/~)@:i.

where now we can take out the common subexpression to get:

maxpal3=:[:('0123456789'&i.@:|:@:":@:,. (*./@:(= |.)@:[ >./@:# ])
])~.@:,@:(*/~)@:i.

   maxpal 1000
906609
   maxpal1 1000
906609
   maxpal2 1000
906609
   maxpal3 1000
906609

Timing:

   6!:2 'maxpal 10000' 
12.977
   6!:2 'maxpal1 10000' 
16.3638
   6!:2 'maxpal2 10000' 
14.3939
   6!:2 'maxpal3 10000' 
10.7871


-- 
View this message in context: 
http://old.nabble.com/Code-review---Euler-tp26659423s24193p26686567.html
Sent from the J Programming mailing list archive at Nabble.com.

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to