Yuvaraj Raghuvir wrote:
> 1) For the J pattern I have found, is there a way to use ^: or $: forms?
> 2) Based on the math behind the Java Code, is there a different way to
> achieve this result? I am not able to formulate the problem so I did it the
> debugging way.
If this is the kind of output you want:
> t4 i.2
> 0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
and you wanted to use ^: then you could say something like:
t =: >: (, >:)@:]^:[ 0:
or, more directly:
t =: +/"1...@#:@:i.@:(2 ^ >:)
for example:
t 2
0 1 1 2 1 2 2 3
t 3
0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
t 4
0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5
But I'm not sure this is what you're asking for, because I haven't read the
Java or J code yet - I just took the last line of your
message as the desired output. And I'm no mathematician, but when I
encounter an interesting integer sequence I turn to OEIS for
insight.
For example, in this case, I copied your longest sequence (quoted above),
inserted a comma between each number, and pasted it into
the search box at the top of http://www.research.att.com/~njas/sequences/
and was directed to A000120:
http://www.research.att.com/~njas/sequences/A000120
That sequence is entitled "number of 1's in binary expansion of n", which I
mechanically translated to J to produce the 2nd version,
above. A little further down the page, we read:
To construct the sequence, start with 0 and use the rule: If k>=0 and
a(0),a(1),...,a(2^k-1)
are the 2^k first terms, then the next 2^k terms are
a(0)+1,a(1)+1,...,a(2^k-1)+1.
- Benoit Cloitre, Jan 30 2003
which is how I derived the first (^: based) version. So you see I didn't have
to read the code or understand its math (and
refactor) to reproduce its results. A $: based version would be pretty
easy, and is left as an exercise for those want to learn
to use OEIS.
-Dan
PS: Incidentally, it took me much longer to write this message than it did to
write the J code after reading the OEIS entry (the
time for that was de minimus).
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm