It might be worthwhile taking a look at the different display forms of the veb collatz. For example, consider the atomicrepresentation:
5!:1<'collatz' ┌────────────────────────────────────────────────┐ │┌─┬────────────────────────────────────────────┐│ ││"│┌────────────────────────────────────┬─────┐││ ││ ││┌──┬───────────────────────────────┐│┌─┬─┐│││ ││ │││@.│┌───────────────┬─────────────┐│││0│0││││ ││ │││ ││┌─┬───────────┐│┌─┬─────────┐│││└─┴─┘│││ ││ │││ │││0│┌────┬────┐│││&│┌─────┬─┐││││ │││ ││ │││ │││ ││cole│colo││││ ││┌─┬─┐│|│││││ │││ ││ │││ │││ │└────┴────┘│││ │││0│2││ │││││ │││ ││ │││ ││└─┴───────────┘││ ││└─┴─┘│ │││││ │││ ││ │││ ││ ││ │└─────┴─┘││││ │││ ││ │││ ││ │└─┴─────────┘│││ │││ ││ │││ │└───────────────┴─────────────┘││ │││ ││ ││└──┴───────────────────────────────┘│ │││ ││ │└────────────────────────────────────┴─────┘││ │└─┴────────────────────────────────────────────┘│ └────────────────────────────────────────────────┘ Working inwards from the outside, we see that the top level control is the rank adverb. Its arguments are represented as a gerund and the noun 0 (the first '0' says that this is a noun, the second is the value of the noun). The top level control in the gerund is the @. conjunction. And http://www.jsoftware.com/help/dictionary/d621.htm says that the rank of @. is determined by the rank of its right verb, which leads to your question. But what is that right verb? According to the diagram, above, the right verb for @. has & as its top level control. And according to http://www.jsoftware.com/help/dictionary/d630n.htm the monadic rank of 2&| is infinite. (Hopefully you do not consider this approach to be too tedious - or, if so, hopefully you skipped down to the end and then read the last couple paragraphs.) Thanks, -- Raul On Tue, Mar 4, 2014 at 10:07 AM, Jon Hough <[email protected]> wrote: > 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.htm Therefore 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: [email protected] > > To: [email protected] > > 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 > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
