At 13:20 -0800 2009/12/07, 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
Speed over accuracy/completeness - not really a great idea... Of
course, one could assume that the largest palindrome, in products of
3 digit numbers, is 6 digits (there are 279, 6 digit palindromes, in
the set). Given that, one could save a little more time by not
looking at numbers less than 100000 - IMOH such "optimizations"
shouldn't be encouraged.
The cost in time to do it more correctly isn't too bad - e.g.
ptpal =: 3 : '_".(*./"1 z=|."1(,.+/"1 '' ''=z)|."1 z)#z=.":,.~.,*/~i.y'
NB. ptpal for ProductTablePalindrome
adding to the timing table above:
Time Expression
.... .................
0.044 >./ ptpal 1000
Fun to play with other numbers too, e.g.
ptpal 12
0 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 121
$ptpal 1000
957
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm