Fractran: Difference between revisions

Added Factor
(Added Befunge example.)
(Added Factor)
Line 898:
67,71,73,79,83,89,97]
 
</pre>
 
=={{header|Factor}}==
<lang factor>USING: io kernel math math.functions math.parser multiline
prettyprint sequences splitting ;
IN: rosetta-code.fractran
 
STRING: fractran-string
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
;
 
: fractran-parse ( str -- seq )
" \n" split [ string>number ] map ;
: fractran-step ( seq n -- seq n'/f )
2dup [ * integer? ] curry find nip dup [ * ] [ nip ] if ;
: fractran-run-full ( seq n -- )
[ dup ] [ dup . fractran-step ] while 2drop ;
: fractran-run-limited ( seq n steps -- )
[ dup pprint bl fractran-step ] times 2drop nl ;
: fractran-primes ( #primes seq n -- )
[ pick zero? ] [
dup 2 logn dup [ floor 1e-9 ~ ] [ 1. = not ] bi and [
dup 2 logn >integer pprint bl [ 1 - ] 2dip
] when fractran-step
] until 3drop nl ;
: main ( -- )
fractran-string fractran-parse 2
[ "First 20 numbers: " print 20 fractran-run-limited nl ]
[ "First 20 primes: " print [ 20 ] 2dip fractran-primes ]
2bi ;
MAIN: main</lang>
{{out}}
<pre>
First 20 numbers:
2 15 825 725 1925 2275 425 390 330 290 770 910 170 156 132 116 308 364 68 4
 
First 20 primes:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
</pre>
 
1,827

edits