Smallest square that begins with n: Difference between revisions

→‎Python: add a simpler alternative
(→‎Python: add a simpler alternative)
Line 2,125:
 
=={{header|Python}}==
===Iterate over prefixes===
<syntaxhighlight lang="python">'''First square prefixed by digits of N'''
 
Line 2,209 ⟶ 2,210:
484
49</pre>
===Iterate over squares===
Generate each square (and its prefixes) only once, and stop when the dict has enough entries.
<syntaxhighlight lang="python">from itertools import accumulate, count
 
d = {}
for q in accumulate(count(1, 2)):
k = q
while k > 0 and k not in d:
if k < 50: d[k] = q
k //= 10
if len(d) == 49: break
 
print(*map(d.get, range(1, 50)))</syntaxhighlight>
{{out}}
<pre>1 25 36 4 529 64 729 81 9 100 1156 121 1369 144 1521 16 1764 1849 196 2025 2116 225 2304 2401 25 2601 2704 289 2916 3025 3136 324 3364 3481 35344 36 3721 3844 3969 400 41209 4225 4356 441 45369 4624 4761 484 49</pre>
 
=={{header|Quackery}}==
559

edits