Fractran: Difference between revisions
Content added Content deleted
Line 2,154: | Line 2,154: | ||
'''Solution''' |
'''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) |
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) fractran is defined as a fixed tacit adverb (that is, a stateless point-free functional), |
||
<lang j> |
<lang j>fractran=. (((({~ (1 i.~ (= <.)))@:* ::]^:)(`]))(".@:('1234567890r ' {~ '1234567890/ '&i.)@:[`))(`:6)</lang> |
||
The argument of |
The argument of fractran specifies a limit for the number of steps; if the limit is boxed the intermediate results are also included in the result. |
||
'''Example''' |
'''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) |
<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) fractran 2 |
||
2 15 825 725 1925 2275 425 390 330 290 770 910 170 156 132</lang> |
2 15 825 725 1925 2275 425 390 330 290 770 910 170 156 132</lang> |
||
Line 2,168: | Line 2,168: | ||
'''Extra credit''' |
'''Extra credit''' |
||
The prime numbers are produced via the adverb primes; its argument has the same specifications as the argument for the |
The prime numbers are produced via the adverb primes; its argument has the same specifications as the argument for the fractran adverb (which is used in its definition), |
||
<lang j>primes=. ('fractan'f.) ((1 }. 2 ^. (#~ *./@:e.&2 0"1@:q:))@:) |
<lang j>primes=. ('fractan'f.) ((1 }. 2 ^. (#~ *./@:e.&2 0"1@:q:))@:) |
||
Line 2,183: | Line 2,183: | ||
'''Turing completeness of J's stateless point-free dialect''' |
'''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 |
When _ is the limit argument (i.e., when no limit is imposed) the run will halt according to the FRACTRAN 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 FRACTRAN associated verb (function) is, |
||
<lang j> _ |
<lang j> _ fractran |
||
".@:('1234567890r ' {~ '1234567890/ '&i.)@:[ ({~ (1 i.~ (= <.)))@:* ::]^:_ ]</lang> |
".@:('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 |
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 FRACTRAN verb becomes, |
||
<lang j> |
<lang j>FRACTRAN=. ({~ (1 i.~ (= <.)))@:* ::]^:_</lang> |
||
which is an indirect concise confirmation that J's fixed tacit dialect is Turing complete. |
which is an indirect concise confirmation that J's fixed tacit dialect is Turing complete. |
||
In the following example, |
In the following example, FRACTRAN 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 |
<lang j> 455r33 11r13 1r11 3r7 11r2 1r3 FRACTRAN 11664 |
||
59604644775390625</lang> |
59604644775390625</lang> |
||