p. is a direct way to take a Collatz step. I was surprised that it's
slower than the constants added to the table
0 1 + 0.5 3 */y
collatzv_pdot =: [: <. 2&|`((2 2$0 0.5 1 3)&(p."1))} NB. slow
And I found that a tacit version of Roger's
collatzv=: 3 : '<. (2|y)} 0 1 + 0.5 3 */y'
collatzv_tacit =: [: <. 2&|`(0 1 + 0.5 3&(*/))}
uses 1r3 less memory.
I also tried item amend to select the polynomial coefficients so that p.
would evaluate only the necessary polynomials. This idea, for some
problems may be a good way to sidestep the "custom verb operating
atomically" performance issue, made a slow collatzv.
See the last paragraph of loopless code 4 in j for c :
http://www.jsoftware.com/docs/help701/jforc/loopless_code_iv_irregular_o.htm#_Toc191734384
Date: Tue, 5 Aug 2014 17:25:58 -0700
From: greg heil<[email protected]>
To: Programming forum<[email protected]>
Subject: Re: [Jprogramming] Memoizing (Project Euler problem 14)
Message-ID:
<CAGnL7rY=9QCfnT9ZjaXKTXhM8Pc=cmexkz4irrxmq3fub_q...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
>An interesting alternative to the verbs Roger expressed on page
http://jsoftware.com/jwiki/Essays/Collatz%20Conjecture)
collatz=: -:`(>:@(3&*))`1: @. (1&= + 2&|)
collatzv=: 3 : '<. (2|y)} 0 1 + 0.5 3 */y'
are
cg=:-:`(>:++:)`1:@.(1&=+2&|)
cgv=: 13 : '(2|y)} (<.-:)`(>:++:)`:0 y'
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm