When a man asks you if he should use a rifle or a pistol to shoot himself
in the foot, a better answer than "rifle" or "pistol" is probably "don't do
that".

Which is to say, it's not clear that you're asking the right question.  So,
rather than simply answer it, I'll try to point you toward a better
question.

Since you're concerned about the performance of this, let's define
something to measure both the time and space consumed:

   ts=: (6!:2),7!:2

Ensure that we get the same result for both usages:

   ((i.20) next 1 0) -: 20 next@]^:(i.@[) 1 0
1

Look at performance stats for some values:

   ts &> '(i. nn) next 1 0';'nn next@]^:(i.@[) 1 0' [ nn=. 20
2.13473e_5  6528
1.31368e_5 12032
   ts &> '(i. nn) next 1 0';'nn next@]^:(i.@[) 1 0' [ nn=. 100
6.07576e_5 39552
8.45681e_5 49664
   ts &> '(i. nn) next 1 0';'nn next@]^:(i.@[) 1 0' [ nn=. 1000
 0.0619301 271232
0.00179563 281344

You had expressed concern about running this for thousands of iterations
and this is in that ballpark - it takes a small fraction of a second.
Re-running this last one shows us these two are basically the same,
performance-wise:

    ts &> '(i. nn) next 1 0';'nn next@]^:(i.@[) 1 0' [ nn=. 1000
 0.0016577 271232
0.00173693 281344

So, to head toward a better question, one that should precede any
performance ones, how do the results look - are they right?  Looking at the
last answer:

   $ans=. nn next@]^:(i.@[) 1 0
1000 2
   _1{ans
_ _

We've exceeded (64-bit) integer bounds and have reached infinity.  At what
point did this happen?

   1 i.~ ans +./ . = _
442
   441 442{ans
1.76105e308 1.32079e308
          _           _

So, after 442 iterations, the answers are all the same.  If these are good
answers, the optimization is obvious.  However, you probably want better
answers than this, so you might use J's extended integers.

   ts 'ans=. nn next@]^:(i.@[) 1 0x'
0.059577 2.7927e6
   441 442{ans
1761050914342066913749490883349768340654108757029821784038978394775957143837188580101920813047861246062919937006679205087207645373833816081752192099271883531401925240639016375434235687122588730785905394280441643059103294109526800114879852631199652973284406...
8805254571710334568747454416748841703270543785149108920194891973879785719185942900509604065239306230314599685033396025436038226869169080408760960496359417657009626203195081877171178435612943653929526971402208215295516470547634000574399263155998264866422030...
   _1{ans
1866527237006437757980179089447634339234182892743416049243428679591933821951551268908880654195762204719189979860648485248430975042708064789683041634407861512468821332276650300547990151971803663022200981592530226236481025029591867581610356542250830207621396...
   10^._1{ans
698.271 698.146

Since one of the elements of the argument vector, 0x, is extended, the
entire vector is promoted to be extended, so are the answers.  Of course,
there is a performance penalty for getting a result that reaches to almost
700 digits.







On Thu, Dec 20, 2012 at 9:43 AM, alessandro codenotti <cod...@live.it>wrote:

>
> The answer expected is exactly the one provided by Henry's code which, for
> the first 10 terms, just copy/pasting from the J console, is:
>
>    next=.5&(0 3 + *)
>    (i. 10) next 1 0
>       1       0
>       5       3
>      25      18
>     125      93
>     625     468
>    3125    2343
>   15625   11718
>   78125   58593
>  390625  292968
> 1953125 1464843
>
> What I'm now speaking about is only how to improve this code, since it
> seems wasting a huge amount of time too me, though I'm not sure about it,
> being a beginner in J!
>
> Alessandro
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>



-- 
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to