Smallest square that begins with n: Difference between revisions

→‎{{header|TXR}}: Condense vertically; don't fill [out 0] which is not required.
(→‎{{header|TXR}}: New section.)
(→‎{{header|TXR}}: Condense vertically; don't fill [out 0] which is not required.)
Line 2,395:
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 5049 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 49) (n 1) (sq 1) (st 2) (out (vector 50)))
((plusp cnt) (outeach (vector(x 1..50))
(put-line (pic "## ########" x [out x]))))
(n 0)
((inc sq 0st) (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))
(while* (plusp xsq)
Line 2,465 ⟶ 2,459:
48 484
49 49</pre>
 
 
=={{header|VTL-2}}==
543

edits