Practical numbers: Difference between revisions

→‎{{header|Pascal}}: new MarkSum some of divisors.Extremly faster than before.
m (→‎{{header|Pascal}}: modified with checking, if all sum of numbers<n are found)
(→‎{{header|Pascal}}: new MarkSum some of divisors.Extremly faster than before.)
Line 127:
</pre>
=={{header|Pascal}}==
simple brute force.InspiredMarking sum of divs by [[Practicalshifting numbers#Python:_Faster_version]]the withformer sum by Limitsthe forthe speednext updivisor.
<lang pascal>program practicalnumbers;
{$IFDEF FPC}
Line 145:
Divs: tDivs;
HasSum :array of byte;
LowLimit,
HighLimit : Uint32;
 
function GetDivisors(n:NativeInt):tdivs;
Line 181 ⟶ 179:
end;
 
procedure SumUpMarkSum(sum,in:UInt32Uint32);
//mark sum and than shift by next divisor
//for practical every sum must be marked
var
phs : pByte;
idx,j,maxlimit,delta : INt32;
Begin
p[sum]hs := 1; //@HasSum[sum0] := 1;
if Lowlimit > HighLimit then
hs[0] := EXIT1;
ifmaxlimit i>0:= then0;
for idx := 0 to Divs.DivsMaxIdx do
Begin
SumUp(sum+delta := Divs.DivsVal[iidx],i-1);
SumUp(sum,i-1);
//shift the values by delta via OR
SumUp(sum+Divs.DivsVal[i],i-1);
For j := maxlimit downto 0 do
end
hs[j+delta] := hs[j+delta] or hs[j];
else
maxlimit += delta;
begin
pIF :=maxlimit @HasSum[0];>n then
BREAK;
p[sum] := 1; //HasSum[sum] := 1;
p[sum+1] := 1;//HasSum[sum+1] := 1;
while p[LowLimit] <> 0 do
inc(LowLimit);
end;
end;
Line 220 ⟶ 219:
if sum >= i then
Begin
LowLimit :=0;
HighLimit := i;
setlength(HasSum,sum+1);
SumUpMarkSum(0,Divs.DivsMaxIdxi);
while (i>= 0) AND (HasSum[i]<>0) do
dec(i);
Line 240 ⟶ 237:
 
var
T0 : Int64;
n,cnt : NativeInt;
Begin
cnt := 11;
For n := 1 to 333330 do
if isPractical(n) then
begin
Line 260 ⟶ 256:
OutIsPractical(66666);
OutIsPractical(720);
n := 1441440;// see anti-primes
T0 := GetTickCount64;
OutIsPractical(5184n);
writeln(n,'5184 takeshas ',(GetTickCount64-T0)/1000:0:3Divs.DivsMaxIdx+1,' sproper divisors');
end.</lang>
</lang>
{{out}}
<pre> TIO.RUN. 7 rows of 11 columns
Line 278 ⟶ 275:
66666 is not practical
720 is practical
51841441440 is practical
1441440 has 287 proper divisors
5184 takes 9.333 s
 
RealUser time: 90.581149 s CPU share: 99.3003 %</pre>
 
=={{header|Perl}}==
Anonymous user