Fractran: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added whitespace and changed some comments, used templates for the output sections.)
Line 2,763: Line 2,763:
numeric digits 2000 /*be able to handle larger numbers. */
numeric digits 2000 /*be able to handle larger numbers. */
parse arg N terms fracs /*obtain optional arguments from the CL*/
parse arg N terms fracs /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=2 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 2 /*Not specified? Then use the default.*/
if terms=='' | terms=="," then terms=100 /* " " " " " " */
if terms=='' | terms=="," then terms= 100 /* " " " " " " */
if fracs='' then fracs= '17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23,',
if fracs='' then fracs= '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'
'77/19, 1/17, 11/13, 13/11, 15/14, 15/2, 55/1'
/* [↑] The default for the fractions. */
/* [↑] The default for the fractions. */
f=space(fracs,0) /*remove all blanks from the FRACS list*/
f= space(fracs, 0) /*remove all blanks from the FRACS list*/
do #=1 while f\==''; parse var f n.# '/' d.# "," f
do #=1 while f\==''; parse var f n.# '/' d.# "," f
end /*#*/ /* [↑] parse all the fractions in list*/
end /*#*/ /* [↑] parse all the fractions in list*/
#=#-1 /*the number of fractions just found. */
#= # - 1 /*the number of fractions just found. */
say # 'fractions:' fracs /*display number and actual fractions. */
say # 'fractions:' fracs /*display number and actual fractions. */
say 'N is starting at ' N /*display the starting number N. */
say 'N is starting at ' N /*display the starting number N. */
Line 2,780: Line 2,780:
if N//d.k\==0 then iterate /*Not an integer? Then ignore it. */
if N//d.k\==0 then iterate /*Not an integer? Then ignore it. */
say right('term' j, 35) "──► " N /*display the Nth term with the N. */
say right('term' j, 35) "──► " N /*display the Nth term with the N. */
N=N % d.k * n.k /*calculate next term (use %≡integer ÷)*/
N= N % d.k * n.k /*calculate next term (use %≡integer ÷)*/
iterate j /*go start calculating the next term. */
iterate j /*go start calculating the next term. */
end /*k*/ /* [↑] if an integer, we found a new N*/
end /*k*/ /* [↑] if an integer, we found a new N*/
end /*j*/ /*stick a fork in it, we're all done. */</lang>
end /*j*/ /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; using the default input:
{{out|output|text=&nbsp; when using the default input:}}
<pre style="height:63ex">
<pre style="height:63ex">
14 fractions: 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
14 fractions: 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
Line 2,894: Line 2,894:
Programming note: &nbsp; if the number of terms specified (the 2<sup>nd</sup> argument) is negative, then only powers of two are displayed.
Programming note: &nbsp; if the number of terms specified (the 2<sup>nd</sup> argument) is negative, then only powers of two are displayed.
<lang rexx>/*REXX program runs FRACTRAN for a given set of fractions and from a specified N. */
<lang rexx>/*REXX program runs FRACTRAN for a given set of fractions and from a specified N. */
numeric digits 999; w=length(digits()) /*be able to handle gihugeic numbers. */
numeric digits 999; w= length( digits() ) /*be able to handle gihugeic numbers. */
parse arg N terms fracs /*obtain optional arguments from the CL*/
parse arg N terms fracs /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=2 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 2 /*Not specified? Then use the default.*/
if terms=='' | terms=="," then terms=100 /* " " " " " " */
if terms=='' | terms=="," then terms= 100 /* " " " " " " */
if fracs='' then fracs= '17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23,',
if fracs='' then fracs= '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'
'77/19, 1/17, 11/13, 13/11, 15/14, 15/2, 55/1'
/* [↑] The default for the fractions. */
/* [↑] The default for the fractions. */
f=space(fracs, 0) /*remove all blanks from the FRACS list*/
f=space(fracs, 0) /*remove all blanks from the FRACS list*/
do #=1 while f\==''; parse var f n.# '/' d.# "," f
do #=1 while f\==''; parse var f n.# '/' d.# "," f
end /*#*/ /* [↑] parse all the fractions in list*/
end /*#*/ /* [↑] parse all the fractions in list*/
#=#-1 /*adjust the number of fractions found.*/
#= # - 1 /*adjust the number of fractions found.*/
tell= terms>0 /*flag: show number or a power of 2.*/
tell= terms>0 /*flag: show number or a power of 2.*/
!.=0; _=1 /*the default value for powers of 2. */
!.= 0; _= 1 /*the default value for powers of 2. */
if \tell then do p=1 until length(_)>digits(); _=_+_; !._=1
if \tell then do p=1 until length(_)>digits(); _= _ + _; !._= 1
if p==1 then @._=left('',w+9) "2**"left(p,w) ' '
if p==1 then @._= left('', w + 9) "2**"left(p, w) ' '
else @._='(prime' right(p,w)") 2**"left(p,w) ' '
else @._= '(prime' right(p, w)") 2**"left(p, w) ' '
end /*p*/ /* [↑] build powers of 2 tables. */
end /*p*/ /* [↑] build powers of 2 tables. */
L=length(N) /*length in decimal digits of integer N*/
L= length(N) /*length in decimal digits of integer N*/
say # 'fractions:' fracs /*display number and actual fractions. */
say # 'fractions:' fracs /*display number and actual fractions. */
say 'N is starting at ' N /*display the starting number N. */
say 'N is starting at ' N /*display the starting number N. */
if tell then say terms ' terms are being shown:' /*display hdr.*/
if tell then say terms ' terms are being shown:' /*display header.*/
else say 'only powers of two are being shown:' /* " " */
else say 'only powers of two are being shown:' /* " " */
q='(max digits used:' /*a literal used in the SAY below. */
q='(max digits used:' /*a literal used in the SAY below. */


do j=1 for abs(terms) /*perform DO loop once for each term. */
do j=1 for abs(terms) /*perform DO loop once for each term. */
do k=1 for # /* " " " " " " fraction*/
do k=1 for # /* " " " " " " fraction*/
if N//d.k\==0 then iterate /*Not an integer? Then ignore it. */
if N//d.k\==0 then iterate /*Not an integer? Then ignore it. */
if tell then say right('term' j, 35) "──► " N /*display Nth term and N.*/
if tell then say right('term' j, 35) "──► " N /*display Nth term and N.*/
else if !.N then say right('term' j,15) "──►" @.N q right(L,w)") " N
else if !.N then say right('term' j,15) "──►" @.N q right(L,w)") " N
N=N % d.k * n.k /*calculate next term (use %≡integer ÷)*/
N= N % d.k * n.k /*calculate next term (use %≡integer ÷)*/
L=max(L, length(N)) /*the maximum number of decimal digits.*/
L= max(L, length(N) ) /*the maximum number of decimal digits.*/
iterate j /*go start calculating the next term. */
iterate j /*go start calculating the next term. */
end /*k*/ /* [↑] if an integer, we found a new N*/
end /*k*/ /* [↑] if an integer, we found a new N*/
end /*j*/ /*stick a fork in it, we're done. */</lang>
end /*j*/ /*stick a fork in it, we're done. */</lang>
'''output''' &nbsp; using the input of: &nbsp; <tt> , &nbsp; -50000000 </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> , &nbsp; -50000000 </tt>}}
<br>(negative fifty million)
<br>(negative fifty million)
<pre>
<pre>