Smallest square that begins with n: Difference between revisions
m
→{{header|Phix}}: use pygments, added trans Pascal (neat answer, btw)
(→{{header|Perl}}: prepend Free Pascal version. Extreme reduced runtime.) |
m (→{{header|Phix}}: use pygments, added trans Pascal (neat answer, btw)) |
||
Line 2,174:
=={{header|Phix}}==
<!--
with javascript_semantics
constant lim = 50
sequence res = repeat(0,lim)
integer n = 1, found = 0
while found<lim do
integer n2 = n*n
while n2 do
if n2<=lim and res[n2]=0 then
found += 1
res[n2] = n
end if
n2 = floor(n2/10)
end while
n += 1
end while
res = columnize({tagset(lim),sq_power(res,2),apply(true,sprintf,{{"(%d^2)"},res})})
printf(1,"Smallest squares that begin with 1..%d:\n%s\n",
▲<!--</syntaxhighlight>-->
{lim,join_by(apply(true,sprintf,{{"%2d: %5d %-8s"},res}),10,5)})
</syntaxhighlight>
{{out}}
<pre>
Line 2,207 ⟶ 2,209:
10: 100 (10^2) 20: 2025 (45^2) 30: 3025 (55^2) 40: 400 (20^2)
</pre>
{{trans|Pascal}}
Same output as the Pascal entry, slight tidy
<syntaxhighlight lang="phix">
with javascript_semantics
function LowSquareStartN(integer n)
-- Find lowest square that matches n
atom sqrtN = sqrt(n),
sqrtN_10 = sqrt(n*10)
do
for res in {trunc(sqrtN),trunc(sqrtN_10)} do
for k=0 to 1 do
atom mySqr := res*res
while mySqr>n do mySqr = floor(mySqr/10) end while
if mySqr = n then return res end if
res += 1
end for
end for
sqrtN *= 10;
sqrtN_10 *= 10;
until sqrtN > 10*n;
?9/0
end function
procedure SquareStartsN()
-- Find smallest square that begins with N
integer t
printf(1,"Test 1 .. 49\n")
for i=1 to 49 do
t := LowSquareStartN(i)
printf(1,"%7d%n",{t*t,mod(i,10)=0})
end for
printf(1,"\n\nTest 999,991 .. 1,000,000\n")
for i=999991 to 1000*1000 do
t := LowSquareStartN(i)
printf(1,"%10d:%10d->%14d\n",{i,t,t*t})
end for
puts(1,"\n")
end procedure
SquareStartsN()
</syntaxhighlight>
=={{header|Picat}}==
|