Anonymous user
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.
<lang pascal>program practicalnumbers;
{$IFDEF FPC}
Line 145:
Divs: tDivs;
HasSum :array of byte;
function GetDivisors(n:NativeInt):tdivs;
Line 181 ⟶ 179:
end;
procedure
//mark sum and than shift by next divisor
//for practical every sum must be marked
var
idx,j,maxlimit,delta : INt32;
Begin
hs[0] :=
for idx := 0 to Divs.DivsMaxIdx do
Begin
//shift the values by delta via OR
▲ SumUp(sum+Divs.DivsVal[i],i-1);
For j := maxlimit downto 0 do
hs[j+delta] := hs[j+delta] or hs[j];
maxlimit += delta;
BREAK;
▲ p[sum] := 1; //HasSum[sum] := 1;
end;
end;
Line 220 ⟶ 219:
if sum >= i then
Begin
setlength(HasSum,sum+1);
while (i>= 0) AND (HasSum[i]<>0) do
dec(i);
Line 240 ⟶ 237:
var
n,cnt : NativeInt;
Begin
cnt := 11;
For n := 1 to
if isPractical(n) then
begin
Line 260 ⟶ 256:
OutIsPractical(66666);
OutIsPractical(720);
n := 1441440;// see anti-primes
OutIsPractical(
writeln(n,'
end.
</lang>
{{out}}
<pre> TIO.RUN. 7 rows of 11 columns
Line 278 ⟶ 275:
66666 is not practical
720 is practical
1441440 has 287 proper divisors
=={{header|Perl}}==
|