Numbers which are not the sum of distinct squares: Difference between revisions

m
→‎{{header|Free Pascal}}: OK,"Do not use magic numbers or pre-determined limits" therefore FindLongestContiniuosBlock
(added =={{header|Pascal}}==)
m (→‎{{header|Free Pascal}}: OK,"Do not use magic numbers or pre-determined limits" therefore FindLongestContiniuosBlock)
Line 65:
SumOfSquare-2, SumOfSquare-3,SumOfSquare-6,...SumOfSquare-108,SumOfSquare-112,SumOfSquare-128<br>
<lang pascal>program practicalnumbers;
{$IFDEF Windows} {$APPTYPE CONSOLE} {$ENDIF}
{$APPTYPE CONSOLE}
{$ENDIF}
var
HasSum: array of byte;
function FindLongestContiniuosBlock(startIdx,MaxIdx:NativeInt):NativeInt;
var
hs0 : pByte;
l : NativeInt;
begin
l := end0;
hs0 := @HasSum[0];
for startIdx := startIdx to MaxIdx do
Begin
IF hs0[startIdx]=0 then
BreakBREAK;
inc(l);
end;
FindLongestContiniuosBlock := l;
end;
 
function SumAllSquares(Limit: Uint32):NativeInt;
Line 75 ⟶ 88:
var
hs0, hs1: pByte;
idx, j, maxlimit, delta,len1TopDownMaxContiniuos,MaxConOffset: NativeInt;
begin
len1TopDownMaxContiniuos := 0;
MaxConOffset := 0;
maxlimit := 0;
hs0 := @HasSum[0];
hs0[0] := 1; //has sum of 0*0
writeln('number longest block sum of');
writeln(' starting at 129 squares');
idx := 1;
 
writeln('number longest block offset longest sum of');
writeln(' starting at 129 block squares');
while idx <= Limit do
begin
delta := idx*idx;
//delta is within the continiuos range than break
If len1TopDown(MaxContiniuos-129MaxConOffset) > delta then
Break;
BREAK;
 
//mark oldsum+ delta with oldsum
hs1 := @hs0[delta];
for j := maxlimit downto 0 do
hs1[j] := hs1[j] or hs0[j];
 
maxlimit := maxlimit + delta;
 
//search for a block of only '1' in this block all numbers are possible sum of squarenumbers
len1TopDownj :=0 MaxConOffset;
repeat
for j := 129 to maxlimit do
delta := FindLongestContiniuosBlock(j,maxlimit);
Begin
IF hs0[j]=0delta>MaxContiniuos then
BREAK;begin
inc(len1TopDown) MaxContiniuos:= delta;
MaxConOffset := j;
end;
end;
writeln(idx:3,len1TopDown:14,maxlimit:14);
inc(j,delta+1);
foruntil j := 129 to> (maxlimit do-delta);
writeln(idx:34,len1TopDownMaxConOffset:147,MaxContiniuos:8,maxlimit:148);
inc(idx);
end;
Line 113 ⟶ 134:
n: NativeInt;
begin
Limit := 10025;
sumsquare := 0;
for n := 1 to Limit do
sumsquare := sumsquare+n*n;
writeln('sum of square [1..',limit,'] = ',sumsquare) ;
writeln;
setlength(HasSum,sumsquare+1);
n := SumAllSquares(Limit);
Line 126 ⟶ 149:
setlength(HasSum,0);
{$IFNDEF UNIX} readln; {$ENDIF}
end.</lang>
</lang>
{{out}}
<pre>
sum of square [1..10025] = 3383505525
 
number longest block offset longest sum of
starting at 129 squares
1 0 block 1squares
2 1 0 0 2 1 -> 50,1
3 2 0 0 2 145
4 3 0 0 2 3014
5 4 0 0 2 5530
6 5 0 0 2 9155
7 6 34 09 91 140->34..42
8 7 49 11 3 140 204->49..59
9 8 77 2815 204 285->77..91
10 9 129 128 +2*128+1= 38528 285
11 10 129 249128 +257 = 506385
12 11 129 393249 +257 = 650506
12 129 393 650
13
2,3,6,7,8,11,12,15,18,19,22,23,24,27,28,31,32,33,43,44,47,48,60,67,72,76,92,96,108,112,128,
</pre>
 
=={{header|Phix}}==
As per Raku (but this is using a simple flag array), if we find a block of n<sup><small>2</small></sup> summables,
Anonymous user