collatz =: ([: $: (] , -:@{:)`(] , 1 + 3 * {:)@.(2&|@{:))`]@.(1 = {:)
collatz 1
1
collatz 2
2 1
collatz 3
3 10 5 16 8 4 2 1
collatz 4
4 2 1
collatz 5
5 16 8 4 2 1
collatz 6
6 3 10 5 16 8 4 2 1
Above uses Agenda @. and Self-Reference $: and test for oddness 2&| borrowed
from Phrases. The assumed right argument is the initial part of a Collatz
sequence. Tail {: provides the tail (last term) of a sequence.
Kip Murray
Sent from my iPad
On Oct 25, 2012, at 4:44 AM, Brian Schott <[email protected]> wrote:
> http://www.jsoftware.com/jwiki/Essays/Collatz%20Conjecture
>
> On Thu, Oct 25, 2012 at 5:12 AM, Stefan Stankovic
> <[email protected]> wrote:
>> I have just started learning J and I think the language is wonderful. It is
>> breathtaking.
>>
>> I have a question about a basic thing-how can I create a monad (preferably
>> tacit) which will, given input x,provide a list of numbers which contains
>> the Collatz seqyence for the given x? Thanks in advance.
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm