Smallest square that begins with n: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Added a Haskell version.) |
(→{{header|Python}}: Added a functionally composed solution in Python.) |
||
Line 561: | Line 561: | ||
10: 100 (10^2) 20: 2025 (45^2) 30: 3025 (55^2) 40: 400 (20^2) |
10: 100 (10^2) 20: 2025 (45^2) 30: 3025 (55^2) 40: 400 (20^2) |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
<lang python>'''First square prefixed by digits of N''' |
|||
from itertools import count |
|||
# firstSquareWithPrefix :: Int -> Int |
|||
def firstSquareWithPrefix(n): |
|||
'''The first perfect square prefixed (in decimal) |
|||
by the decimal digits of N. |
|||
''' |
|||
pfx = str(n) |
|||
lng = len(pfx) |
|||
return int( |
|||
next( |
|||
s for s in ( |
|||
str(x * x) for x in count(0) |
|||
) |
|||
if pfx == s[0:lng] |
|||
) |
|||
) |
|||
# ------------------------- TEST ------------------------- |
|||
def main(): |
|||
'''First matches for the range [1..49]''' |
|||
print('\n'.join([ |
|||
str(firstSquareWithPrefix(x)) for x in range(1, 50) |
|||
])) |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
main()</lang> |
|||
{{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|Raku}}== |
=={{header|Raku}}== |