Fractran: Difference between revisions

Line 2,154:
'''Solution'''
 
This is a variation of the previous solution which it is not entirely tacit due to the use of the explicit standard library verb (function) charsub. The adverb (functional) fractanfractran is defined as a fixed tacit adverb (that is, a stateless point-free functional),
 
<lang j>fractanfractran=. (((({~ (1 i.~ (= <.)))@:* ::]^:)(`]))(".@:('1234567890r ' {~ '1234567890/ '&i.)@:[`))(`:6)</lang>
The argument of fractanfractran specifies a limit for the number of steps; if the limit is boxed the intermediate results are also included in the result.
 
'''Example'''
 
<lang j> '17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1' (<15) fractanfractran 2
2 15 825 725 1925 2275 425 390 330 290 770 910 170 156 132</lang>
 
Line 2,168:
'''Extra credit'''
 
The prime numbers are produced via the adverb primes; its argument has the same specifications as the argument for the fractanfractran adverb (which is used in its definition),
 
<lang j>primes=. ('fractan'f.) ((1 }. 2 ^. (#~ *./@:e.&2 0"1@:q:))@:)
Line 2,183:
'''Turing completeness of J's stateless point-free dialect'''
 
When _ is the limit argument (i.e., when no limit is imposed) the run will halt according to the FRACTANFRACTRAN programming language specifications (the run might also be forced to halt if a trivial changeless single cycle, induced by a useless 1/1 fraction, is detected). Thus, the FRACTANFRACTRAN associated verb (function) is,
<lang j> _ fractanfractran
".@:('1234567890r ' {~ '1234567890/ '&i.)@:[ ({~ (1 i.~ (= <.)))@:* ::]^:_ ]</lang>
 
Actually, most of the code above is there to comply with the task's requirement of a "''natural'' format." When J's format for fractions is used the FRACTANFRACTRAN verb becomes,
 
<lang j>FRACTANFRACTRAN=. ({~ (1 i.~ (= <.)))@:* ::]^:_</lang>
 
which is an indirect concise confirmation that J's fixed tacit dialect is Turing complete.
 
In the following example, FRACTANFRACTRAN calculates the product 4 * 6, the initial value 11664 = (2^4)*(3^6) holds 4 in the register associated with 2 and holds 6 in the register associated with 3; the result 59604644775390625 = 5^24 holds the product 24 = 4 * 6 in the register associated with 5,
 
<lang j> 455r33 11r13 1r11 3r7 11r2 1r3 FRACTANFRACTRAN 11664
59604644775390625</lang>
 
Anonymous user