Smallest square that begins with n: Difference between revisions
Content added Content deleted
(Smallest square that begins with n in BASIC256 and True BASIC) |
|||
Line 1,662: | Line 1,662: | ||
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|Picat}}== |
|||
===Recursion and string approach=== |
|||
<lang Picat>import util. |
|||
main => |
|||
println([S : N in 1..49, S = smallest_square(N)]). |
|||
smallest_square(N) = Square => |
|||
smallest_square(N.to_string,1,Square). |
|||
smallest_square(N,S,SS) :- |
|||
SS = S*S, |
|||
find((SS).to_string,N,1,_). |
|||
smallest_square(N,S,SS) :- |
|||
smallest_square(N,S+1,SS).</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> |
|||
===Iterative=== |
|||
<lang Picat>main => |
|||
println([S : N in 1..49, S = smallest_square2(N)]). |
|||
smallest_square2(N) = Ret => |
|||
I = 1, |
|||
Found = false, |
|||
while (Found == false) |
|||
Square = I*I, |
|||
while (Square > N) |
|||
Square := Square // 10 |
|||
end, |
|||
if Square == N then |
|||
Found := I*I |
|||
end, |
|||
I := I + 1 |
|||
end, |
|||
Ret = Found.</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|PL/I}}== |
=={{header|PL/I}}== |