Count in octal: Difference between revisions

→‎J: simplify
(→‎J: simplify)
Line 1,818:
=={{header|J}}==
'''Solution:'''
<syntaxhighlight lang="j"> disp=. ([ echo) '1 '(-.~":) 8&#.inv
(1 + disp)^:_]0x</syntaxhighlight>
 
The full result is not displayable, by design. This could be considered a bug, but is an essential part of this task. Here's how it starts:
 
<syntaxhighlight lang="j"> (1 + disp)^:_]0x
0
1
Line 1,836:
...</syntaxhighlight>
 
The important part of this code is 8&#.inv which converts numbers from internal representation to a sequence of base 8 digits. (We then convert this sequence to characters and remove the delimiting spaces - this gives us the octal values we want to display.)
 
So then we define disp as a word which displays its argument in octal and returns its argument as its result (unchanged).
 
Finally, the <code>^:_</code> clause tells J to repeat this function forever, with <code>(1 + disp)</code>adding 1 to the result each time it is displayed (or at least that clause tells J to keep repeating that operation until it gives the same value back twice in a row - which won't happen - or to stop when the machine stops - like if the power is turned off - or if J is shut down - or...).
 
We use arbitrary precision numbers, not because there's any likelihood that fixed width numbers would ever overflow, but to emphasize that this thing is going to have to be shut down by some mechanism outside the program.
559

edits