Thanks for all the replies.OK, I'm starting to understand more now. I did not know about agenda (@.). Well actually, I read about it in the jsoftware.com dictionary, but I think J is one of those things you have to see in action before it clicks. What I've gone with: cole =. 2&(%~) NB. even case colo =. (1&+)@(3&*) collatz =.cole`colo@.(2&|)"0 NB. Rank 0 lets us work on each element of a list
So my next question is how do I go about iterating this until we reach 1? Incidentally, it seems to me that | is rank 0 (right rank) http://www.jsoftware.com/help/dictionary/d230.htmTherefore, I am puzzled why the verb collatz does not act on each rank 0 element of the noun. Why did I have to explicitly force its right rank to be 0? Regarding iterations,Bo Jacoby kindly mentioned: collatz=:-:&(+2&|*>:&+:) collatz ^:(i.10)17 17 26 13 20 10 5 8 4 2 1 But I'm struggling to understand this verb. Any help explaining this would be appreciated. Or help making my own collatz verb into an iterative function (verb). Regards,Jon > Date: Mon, 3 Mar 2014 18:59:04 -0800 > From: d...@shaw.ca > To: programm...@jsoftware.com > Subject: Re: [Jprogramming] Simple Number Theory > > This might be more readable - I had some bold faced characters so got > the extra dusting of * > > Don > > collatz=:(1 0=2|])#(3*1+]),2%~] > > collatz 4 > > 2 > > collatz 5 > > 18 > > collatz _4 > > _2 > > collatz _5 > > _12 > > > Test for odd/even is 2|nreturning 1 for odd and 0 for even 1 0=2|n will > return 1 0 for odd and 0 1 for even > The (3*1+n) and n%2 terms are a two element result vector > > if odd, 1 0 # returns the odd result and if even 0 1 #returns the even > result. > > In this case I started with the basic (3*n+1),n%2vector and then used > 1 0= 2|n times this(residue is 1 for odd and 0 for even numbers) > the result is(1 0=2|y) # (3*1+y),y%2which works > I wrote this as an explicit verb s=: 13 : '(1 0=2|y)#(3*1+y),y%2' > and typed sto get a tacit form (1 0 = 2 | ]) # (3 * 1 + ]) , 2 %~ ] > generated by J > > attached collatz=:to the front and that was it. > > note that the y is replaced by ] and the y%2 is expressed using 2%~] J > wants the ] on the right of this operation and ~does this > There are other variations on this but this is the simplest I came up with. > > I classify myself as a beginner but I have an APL background and long > ago found C and relatives awkward I do think it is harder to come from > C/C++/Java to J than from APL to J - the thinking is different To me > C++ is oriented towards detailed instructions for the compiler (much of > this is done by the interpreter in J or APL) The problem comes first > in J and more times than not - use of arrays can eliminate a lot of if's > and loops. > > > Don Kelly > > > On 03/03/2014 8:30 AM, Jon Hough wrote: > > Beginner question again.I quick task I set myself was to write ONE > > ITERATION of the Collatz function for a given positive > > integer.http://en.wikipedia.org/wiki/Collatz_conjecture > > > > So my verb is supposed to do 3*n+1 if n is odd and n/2 if n is even.In a > > more imperative/OOE based language (C/C++/Java) I could write this in less > > than a minute. Unfortunately, I fell at the very first hurdle in J. > > I originally wrote my tacit verb for even ints:collatz_even =.2&(%~) > > collatz_even 4 > > 2 > > This works, but I had a terrible time trying to put the brackets in the > > right place. I am not sure why %~ needs to be bracketed. Won't J parse %~ > > as dyadic and "know" that the left operand is 2? > > Next I tried to do the case for odd n: > > collatz_odd =. 1&+@(3&*) collatz_odd 3 > > 10 > > That seems to work ok.Now I am not sure how to do an if statement in J. In > > plain English I want "If n is even do collatz_even else do collatz_odd".The > > verb I wrote to test for even-ness is > > ones =. {:@#: NB. Finds the ones column values. 1 => odd, 0=> even > > 1 = ones 2 > > 0 > > So I have a test but I am not sure how to utilize this test. How should I > > go about doing:"If n is even do collatz_even else do collatz_odd"? > > Thanks and regards,Jon > > > > ---------------------------------------------------------------------- > > For information about J forums seehttp://www.jsoftware.com/forums.htm > > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm