Smallest square that begins with n: Difference between revisions

→‎{{header|TXR}}: New section.
m (→‎{{header|Quackery}}: tweaked code)
(→‎{{header|TXR}}: New section.)
Line 2,389:
46: 4624 (68^2) 47: 4761 (69^2) 48: 484 (22^2) 49: 49 (7^2)
</pre>
 
=={{header|TXR}}==
 
In this solution we avoid calculating squares; no multiplication occurs in the code.
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.
 
<syntaxhighlight lang="txrlisp">(for ((cnt 50)
(out (vector 50))
(n 0)
(sq 0)
(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)
(when (and (< xsq 50) (null [out xsq]))
(set [out xsq] sq)
(dec cnt))
(set xsq (trunc xsq 10)))))</syntaxhighlight>
 
{{out}}
 
<pre> 1 1
2 25
3 36
4 4
5 529
6 64
7 729
8 81
9 9
10 100
11 1156
12 121
13 1369
14 144
15 1521
16 16
17 1764
18 1849
19 196
20 2025
21 2116
22 225
23 2304
24 2401
25 25
26 2601
27 2704
28 289
29 2916
30 3025
31 3136
32 324
33 3364
34 3481
35 35344
36 36
37 3721
38 3844
39 3969
40 400
41 41209
42 4225
43 4356
44 441
45 45369
46 4624
47 4761
48 484
49 49</pre>
 
 
=={{header|VTL-2}}==
543

edits