Count in octal: Difference between revisions

m
J: rephrase, and explain
m (J: rephrase, and explain)
Line 671:
=={{header|J}}==
'''Solution:'''
<lang J> disp=.([:smoutput) [:,@:' '(-.~":"0) 8&#.^:_1
(>:[1+disp)^:_ ]00x</lang>
 
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:
 
<lang j> (>:[1+disp)^:_ ]00x
0
1
Line 688:
11
...</lang>
 
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>(1+disp)^:_</code> clause tells J to repeat this function forever, adding 1 to the result each time (or repeat 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 J is shut down...).
 
We use arbitrary precision numbers, not because there's any likelihood that fixed width numbers would ever overflow, but just to emphasize that this thing is going to have to be shut down by some mechanism outside the program.
 
=={{header|Java}}==
6,962

edits