Fractran: Difference between revisions

2,524 bytes added ,  5 years ago
Line 1,903:
 
=={{header|J}}==
 
===Hybrid version===
'''Solution:'''
<lang j>toFrac=: '/r' 0&".@charsub ] NB. read fractions from string
Line 1,911 ⟶ 1,913:
taskstr fractran15 2
2 15 825 725 1925 2275 425 390 330 290 770 910 170 156 132</lang>
 
===Tacit version===
 
'''Solution'''
 
This is a variation of the previous solution which it is not entirely tacit due to use of the explicit standard library verb (function) charsub. The adverb (functional) is defined as a fixed tacit adverb (that is, a stateless point-free functional),
 
<lang j>fractan=. (((({~ (1 i.~ (= <.)))@:* ::]^:)(`]))(".@:('1234567890r ' {~ '1234567890/ '&i.)@:[`))(`:6)</lang>
The argument of fractan 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) fractan 2
2 15 825 725 1925 2275 425 390 330 290 770 910 170 156 132</lang>
 
'''Extra credit'''
 
The prime numbers are produced via the stateless point-free adverb primes; its argument has the same specifications as the argument for the fractan adverb (which is used in its definition),
 
<lang j>primes=. ('fractan'f.) ((1 }. 2 ^. (#~ *./@:e.&2 0"1@:q:))@:)
'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' (<555555) primes 2
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71</lang>
 
the stateless point-free code of the tacit adverb primes is,
<lang j> primes
((((({~ (1 i.~ (= <.)))@:* ::]^:)(`]))(".@:('1234567890r ' {~ '1234567890/ '&i.)@:[`))(`:6))((1 }. 2 ^. (#~ *./@:e.&2 0"1@:q:))@:)</lang>
 
'''Stateless point-free Turing completeness'''
 
When _ is the limit argument; that is, when no limit is imposed, the run will halt according to the FRACTAN specifications (the run might also halt if a changeless single cycle is detected). Thus, the FRACTAN verb (function) is,
<lang j> _ fractan
".@:('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 FRACTAN verb emulator code is reduced to,
 
<lang j>FRACTAN=. ({~ (1 i.~ (= <.)))@:* ::]^:_</lang>
 
This is a concise confirmation that the fixed tacit dialect of J is Turing complete.
 
In the following example, FACTRAN calculates the product 2 * 3 where the initial value 72 = (2^3)*(3^2) where 3 is in the register associated with 2 and 2 is in the register associated with 3 and the result 15625 = 5^6 holds the product 6 in the register associated with 5.
 
<lang j> 455r33 11r13 1r11 3r7 11r2 1r3 FRACTAN 72
15625</lang>
 
=={{header|Java}}==
Anonymous user