Fractran: Difference between revisions

Content added Content deleted
(→‎{{header|Common Lisp}}: Fix termination problem.)
(→‎{{header|REXX}}: added a version for extra credit. -- ~~~~)
Line 588: Line 588:


=={{header|REXX}}==
=={{header|REXX}}==
Programming note: extra blanks can be inserted in the fractions before and/or after the solidus ['''/'''].
Programming note: extra blanks can be inserted in the fractions before and/or after the solidus ['''/'''].
===showing all terms===
<lang rexx>/*REXX pgm runs FRACTAN for a given set of fractions and from a given N.*/
<lang rexx>/*REXX pgm runs FRACTAN for a given set of fractions and from a given N.*/
numeric digits 1000 /*be able to handle larger nums. */
numeric digits 1000 /*be able to handle larger nums. */
Line 717: Line 718:
term 99 ──► 2128
term 99 ──► 2128
term 100 ──► 1288
term 100 ──► 1288
</pre>

===showing powers of 2 terms===
Programming note: &nbsp; If the number of terms is a negative integer, then only powers of two are displayed.
<lang rexx>/*REXX pgm runs FRACTAN for a given set of fractions and from a given N.*/
numeric digits 1000 /*be able to handle larger nums. */
parse arg N terms fracs /*get optional arguments from CL.*/
if N=='' | N==',' then N=2 /*N specified? No, use default.*/
if terms==''|terms==',' then terms=100 /*TERMS specified? Use default.*/
if fracs='' then fracs= , /*any fractions specified? No···*/
'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'
f=space(fracs,0) /* [↑] use default for fractions.*/
tell= terms>0 /*flag: show # or a power of 2.*/
do i=1 while f\==''; parse var f n.i '/' d.i ',' f
end /*i*/ /* [↑] parse all the fractions.*/
!.=0 /*default value for powers of 2.*/
if \tell then do p=0 until length(_)>100; _=2**p; !._=1; @._='2**'p
end /*p*/ /* [↑] build powers of 2 tables.*/
#=i-1 /*the number of fractions found. */
say # 'fractions:' fracs /*display # and actual fractions.*/
say 'N is starting at ' N /*display the starting number N.*/
if tell then say terms ' terms are being shown:' /*display hdr.*/
else say 'only powers of two are being shown:' /* " " */

do j=1 for abs(terms) /*perform loop once for each term*/
do k=1 for #; if N//d.k\==0 then iterate /*not an integer?*/
if tell then say right('term' j,35) '──► ' N /*display Nth term&N*/
else if !.N then say right('term' j,35) '──► ' N ' ' @.N
N = N * n.k % d.k /*calculate the next term (use %)*/
leave /*go start calculating next term.*/
end /*k*/ /* [↑] if integer, found a new N*/
end /*j*/
/*stick a fork in it, we're done.*/</lang>
'''output''' using the input of: &nbsp; <tt> , -1000000 </tt>
<pre>
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
N is starting at 2
only powers of two are being shown:
term 1 ──► 2 2**1
term 20 ──► 4 2**2
term 70 ──► 8 2**3
term 281 ──► 32 2**5
term 708 ──► 128 2**7
term 2364 ──► 2048 2**11
term 3877 ──► 8192 2**13
term 8069 ──► 131072 2**17
term 11320 ──► 524288 2**19
term 19202 ──► 8388608 2**23
term 36867 ──► 536870912 2**29
term 45552 ──► 2147483648 2**31
term 75225 ──► 137438953472 2**37
term 101113 ──► 2199023255552 2**41
term 117832 ──► 8796093022208 2**43
term 152026 ──► 140737488355328 2**47
term 215385 ──► 9007199254740992 2**53
term 293376 ──► 576460752303423488 2**59
term 327021 ──► 2305843009213693952 2**61
term 428554 ──► 147573952589676412928 2**67
term 507520 ──► 2361183241434822606848 2**71
term 555695 ──► 9444732965739290427392 2**73
term 700064 ──► 604462909807314587353088 2**79
term 808332 ──► 9671406556917033397649408 2**83
term 989527 ──► 618970019642690137449562112 2**89
</pre>
</pre>