Smallest square that begins with n: Difference between revisions

Content added Content deleted
(→‎{{header|TXR}}: New section.)
(→‎{{header|TXR}}: Condense vertically; don't fill [out 0] which is not required.)
Line 2,395: Line 2,395:
We generate successive squares using a recurrence relation.
We generate successive squares using a recurrence relation.


We also avoid doing a <code>starts-with</code> test using digits. Rather, we take each successive square and begin repeatedly dividing it by 10, with a truncating division. Whenever the quotient fits into the range 0 to 49 (valid index for our output table) we check whether the entry at that position is <code>nil</code>. If so, this is the smallest square which begins with the digits of that position and we put it into the table there. When 50 numbers have been placed, indicated by an incrementing counter, the algorithm ends.
We also avoid doing a <code>starts-with</code> test using digits. Rather, we take each successive square and begin repeatedly dividing it by 10, with a truncating division. Whenever the quotient fits into the range 0 to 49 (valid index for our output table) we check whether the entry at that position is <code>nil</code>. If so, this is the smallest square which begins with the digits of that position and we put it into the table there. When 49 numbers have been placed, indicated by an incrementing counter, the algorithm ends. The <code>[out 0]</code> entry is left null.


<syntaxhighlight lang="txrlisp">(for ((cnt 50)
<syntaxhighlight lang="txrlisp">(for ((cnt 49) (n 1) (sq 1) (st 2) (out (vector 50)))
(out (vector 50))
((plusp cnt) (each ((x 1..50))
(put-line (pic "## ########" x [out x]))))
(n 0)
(sq 0)
((inc sq st) (inc st 2) (inc n))
(st 1))
((plusp cnt) (each ((x 1..50))
(put-line (pic "## ########" x [out x]))))
((inc sq st)
(inc st 2)
(inc n))
(let ((xsq sq))
(let ((xsq sq))
(while* (plusp xsq)
(while* (plusp xsq)
Line 2,465: Line 2,459:
48 484
48 484
49 49</pre>
49 49</pre>



=={{header|VTL-2}}==
=={{header|VTL-2}}==