Anonymous user
Practical numbers: Difference between revisions
m
→{{header|alternative}}: only memorizing heigher divisors
(→{{header|Pascal}}: Now without generating sum of allset) |
m (→{{header|alternative}}: only memorizing heigher divisors) |
||
Line 341:
==={{header|alternative}}===
Now without generating sum of allset.
<lang pascal>program
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
Line 348 ⟶ 349:
{$ENDIF}
uses
type
tdivs = record
var
Divs: tDivs;
function CheckIsPractical(var Divs: tdivs; n: Uint32): boolean;
//calc all divisors,calc sum of divisors
var
quot,ug,sq,de: UInt32;
▲ i := 1;
while i*i < n do▼
begin
sq :=
de :=
while sq
quot := n div i;
end;▼
Inc(i);
sq += de;
de := de+2;
end;▼
if sq = n then
begin
if sum + 1 < i then
EXIT(false);
DivsVal[ug] := i;
▲ inc(ug);
end;
▲ end;
if sum + 1 < i then
EXIT(
end;
end;//
result := true;
▲ inc(og);
▲ i := DivsVal[og];
▲ if sum+1 < i then
▲ EXIT(FALSE);
▲ inc(sum,i);
▲ If sum>=n-1 then
▲ EXIT(TRUE);
▲ end;
end;▼
function isPractical(n: Uint32): boolean;
begin
if n
EXIT(True);
if ODD(n) then
EXIT(
end;
procedure OutIsPractical(n: nativeInt);
begin
writeln(n, ' is practical')
else
writeln(n, ' is not practical');
end;
const
var
T0 :
n, col,
begin
col := ColCnt;
if isPractical(n) then
begin
if col = 0 then
writeln;
col := ColCnt;
end;
end;
writeln;
writeln('There are ',
writeln;
OutIsPractical(666);
OutIsPractical(6666);
Line 453 ⟶ 460:
OutIsPractical(954432);
OutIsPractical(720);
OutIsPractical(
OutIsPractical(1441440);
OutIsPractical(99998640);
T0 := GetTickCOunt64;
{$IFDEF WINDOWS} readln;{$ENDIF} ▼
count := 0;
end.</lang>▼
For n := 1 to 1000*1000 do
inc(count,Ord(isPractical(n)));
writeln('Count of practical numbers til 1,000,000 ',count,(GetTickCount64-t0)/1000:8:4,' s');
readln;
{$ENDIF}
{{out}}
<pre> TIO.RUN
Line 476 ⟶ 491:
954432 is not practical
720 is practical
1441440 is practical
99998640 is not practical
Count of practical numbers til 1,000,000 97385 2.1380 s
Real time: 2.277 s CPU share: 99.55 %</pre>
=={{header|Perl}}==
|