Smallest square that begins with n: Difference between revisions

Add Refal
(Add Refal)
(5 intermediate revisions by 4 users not shown)
Line 2,032:
484
49</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE SmallestSquareWithPrefix;
FROM InOut IMPORT WriteCard, WriteLn;
 
VAR n: CARDINAL;
 
PROCEDURE prefix(n, m: CARDINAL): BOOLEAN;
BEGIN
IF n>=m
THEN RETURN n=m
ELSE RETURN prefix(n, m DIV 10)
END
END prefix;
 
PROCEDURE firstPrefixSquare(n: CARDINAL): CARDINAL;
VAR
sq, sqr: CARDINAL;
BEGIN
sqr := 0;
REPEAT
INC(sqr);
sq := sqr*sqr
UNTIL prefix(n, sq);
RETURN sq
END firstPrefixSquare;
 
BEGIN
FOR n := 0 TO 49 DO
WriteCard(firstPrefixSquare(n), 7);
IF n MOD 10 = 9 THEN WriteLn END
END
END SmallestSquareWithPrefix.</syntaxhighlight>
{{out}}
<pre> 1 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|Nim}}==
Line 2,210 ⟶ 2,249:
</pre>
===={{trans|Julia}}====
Storing only the root ( n_running ) of the square so Uint32 suffices for the result.<br>
//Improved 355msversion -> 335 ms<br>
Stop searching as early as possible->minimize count of divisions (~ n*(1+3.162=sqrt(10)) ) -> 71 ms<br>
<syntaxhighlight lang="pascal">program smsq;
increment as long the testsqr didn't change in last digit. Divisions (~ n*(1+1.8662 )) -> 53 ms :-)
<syntaxhighlight lang="pascal">
program smsq;
{$IFDEF FPC}{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$ENDIF}
uses
Line 2,219 ⟶ 2,261:
tRes = array of Uint32;
 
var
maxTestVal : Uint32;
function smsq(n:Uint32):tRes;
// limit n is ~ 1.358E9
var
ksquare,nxtSqr,Pow10 : Uint64;
n_runningn_run,testSqr,found : Uint32;
 
begin
setlength(result,n+1);
fillchar(result[0],length(result)*Sizeof(result[0]),#0);
found := 0;
n_runningsquare := 10;
n_run := 0;
Pow10 := 1;
nxtSqr := 1;//sqr(n_run)+1;
while found < n do
begin
repeat
k := sqr(n_running);
while k >n_run 0 do+=1;
square := sqr(n_run);
begin
until square >= nxtSqr;
if (k <= n) AND (result[k] = 0) then
//bring square into the right place
Begin
testSqr := square div pow10;
result[k] := n_running;
while testSqr > n found += 1;do
end;Begin
kpow10 :*= k div 10;
testSqr := testSqr div 10;
end;
//next square must increase by one digit
inc(n_running);
nxtSqr := (testSqr+1)*pow10;
end
repeat
//no need to test any more
//if found ex. 4567 than 456,45 and 4 already marsquareed
if result[testSqr] <> 0 then
BREAK;
result[testSqr] := n_run;
found += 1;
testSqr := testSqr div 10;
until testSqr = 0;
end;
maxTestVal := n_run;
end;
 
Line 2,259 ⟶ 2,318:
end;
writeln;
writeln('Max test value : ',maxTestVal); ;
writeln;
 
n := 10*1000*1000;
// speed up cpu
Line 2,265 ⟶ 2,327:
smsq(n);
t0 := GetTickCount64-t0;
writeln('check 1..',n,' in ', T0,' ms. Max test value : ',maxTestVal);
END.
</syntaxhighlight>
Line 2,275 ⟶ 2,337:
3136 324 3364 3481 35344 36 3721 3844 3969 400
41209 4225 4356 441 45369 4624 4761 484 49
Max test value : 213
check 1..10000000 in 335 ms
 
check 1..10000000 in 53 ms. Max test value : 31622776
....
check 1..1000000000 in 5174 ms. Max test value : 3162277656
</pre>
 
Line 2,330 ⟶ 2,396:
<syntaxhighlight lang="phix">
with javascript_semantics
constant lim = 5049
sequence res = repeat(0,lim)
integer n = 1, found = 0
Line 2,851 ⟶ 2,917:
 
Same output.
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Table (7 7) <Each FirstPrefixSquare <Iota 1 49>>>;
};
 
Cell {
s.W s.N, <Repeat s.W ' '> <Symb s.N>: e.C,
<Last s.W e.C>: (e.X) e.CI = e.CI;
}
 
Repeat {
0 s.C = ;
s.N s.C = s.C <Repeat <- s.N 1> s.C>;
};
 
Table {
(s.Cols s.CW) e.X = <Table () (s.Cols s.Cols s.CW) e.X>;
(e.Row) (s.Cols s.N s.CW), e.Row: {
= ;
e.Row = <Prout e.Row>;
};
(e.Row) (s.Cols 0 s.CW) e.X =
<Prout e.Row>
<Table () (s.Cols s.Cols s.CW) e.X>;
(e.Row) (s.Cols s.N s.CW) s.I e.X =
<Table (e.Row <Cell s.CW s.I>) (s.Cols <- s.N 1> s.CW) e.X>;
};
 
Each {
s.F = ;
s.F s.I e.X = <Mu s.F s.I> <Each s.F e.X>;
};
 
Iota {
s.End s.End = s.End;
s.Start s.End = s.Start <Iota <+ 1 s.Start> s.End>;
};
 
FirstPrefixSquare {
s.N = <FirstPrefixSquare s.N 1>;
s.N s.Sqr, <* s.Sqr s.Sqr>: s.Sq,
<Symb s.N>: e.Pfx,
<Symb s.Sq>: e.Pfx e.X = s.Sq;
s.N s.Sqr = <FirstPrefixSquare s.N <+ 1 s.Sqr>>;
};
</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|REXX}}==
Line 3,209 ⟶ 3,330:
(return-from search)))
((set k (trunc k 10)))))))</syntaxhighlight>
 
 
=={{header|Uiua}}==
{{works with|Uiua|0.10}}
 
In best YAGNI style, just calculate sufficient squares for the task as specced (up to 220^2)
 
<syntaxhighlight lang="Uiua">
IsPrefix ← =⧻⟜(/+⬚@.=)
⊞◇IsPrefix °⋕↘1⇡50 °⋕.ⁿ2+1⇡220
⊏≡(⊢⊚)
</syntaxhighlight>
{{out}}
<pre>
[1 25 36 4 529 64 729 81 9 100 1156 ...etc...]
</pre>
 
=={{header|VTL-2}}==
2,114

edits