Jump to content

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 practicalnumberspracticalnumbers2;
 
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
Line 348 ⟶ 349:
{$ENDIF}
uses
sysutilsSysUtils;
 
type
tdivs = record
DivsVal : array[0..20481024 - 1] of Uint32;
end;
 
var
Divs: tDivs;
 
function CheckIsPractical(var Divs: tdivs; n: Uint32): boolean;
//calc all divisors,calc sum of divisors
var
i,quot,ug,og sum: UInt32UInt64;
i := 1NativeInt;
sum: UInt64;
quot,ug,sq,de: UInt32;
Begin
 
with Divs do
Beginbegin
sumwith :=Divs 0;do
ug := Low(tdivs.DivsVal);
og := High(tdivs.DivsVal);
i := 1;
while i*i < n do
begin
quotsum := n div i1;
IF n - quot*iug := 0 thenLow(tdivs.DivsVal);
Begini := 2;
sq := if sum+1 < i then4;
de := EXIT(FALSE)5;
while sq DivsVal[og]< :=n quot;do
inc(og);begin
quot := n div i;
if sum+1n <- quot * i = 0 then
inc(ug);begin
If if sum>=n- + 1 < i then
EXIT(FALSEfalse);
inc Inc(sum, i);
i := DivsVal[ogug] := quot;
EXIT Inc(TRUEug);
end;
Inc(i);
sq += de;
de := de+2;
end;
if sq = n then
begin
if sum + 1 < i then
EXIT(false);
DivsVal[ug] := i;
incInc(sum, i);
decInc(ogug);
inc(ug);
end;
inc(i);//check higher
while i*iug <> n0 do
end;
If i*i = n thenbegin
Begin Dec(ug);
if sum+1 < i then:= DivsVal[ug];
if sum + 1 < i then
EXIT(FALSEfalse);
DivsVal[og] := Inc(sum, i);
inc( if sum,i); >= n - 1 then
dec(og) break;
end;
end;//check higherwith
result := true;
while og < High(tDivs.DivsVal) do
beginend;
inc(og);
i := DivsVal[og];
if sum+1 < i then
EXIT(FALSE);
inc(sum,i);
If sum>=n-1 then
EXIT(TRUE);
end;
end;//with
end;
 
function isPractical(n: Uint32): boolean;
begin
Begin
if n= in [1,2] then
EXIT(True);
if ODD(n) then
EXIT(falseFalse);
result Result := CheckIsPractical(Divs, n);
end;
 
procedure OutIsPractical(n: nativeInt);
begin
If if isPractical(n) then
writeln(n, ' is practical')
else
writeln(n, ' is not practical');
end;
 
const
ColCnt = 10;
MAX = 333;
var
T0 : INt64int64;
n, col,count Count: NativeInt;
begin
Begin
col := ColCnt;
countCount := 0;
Forfor n := 1 to MAX do
if isPractical(n) then
begin
writeWrite(n: 5);
incInc(countCount);
decDec(col);
if col = 0 then
Beginbegin
writeln;
col := ColCnt;
end;
end;
writeln;
writeln('There are ',count Count, ' pratical numbers from 1 to ', MAX);
writeln;
 
T0 := GetTickCount64;
 
OutIsPractical(666);
OutIsPractical(6666);
Line 453 ⟶ 460:
OutIsPractical(954432);
OutIsPractical(720);
OutIsPractical(53845184);
OutIsPractical(1441440);
OutIsPractical(99998640);
 
Writeln( (GetTickCount64- T0)/1000:7:5,' s');
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');
{$IFDEF WINDOWS} readln;{$ENDIF}
readln;
{$ENDIF}
end;.
end.</lang>
{{out}}
<pre> TIO.RUN
Line 476 ⟶ 491:
954432 is not practical
720 is practical
53845184 is not practical
1441440 is practical
99998640 is not practical
Count of practical numbers til 1,000,000 97385 2.1380 s
0.00000 s</pre>
 
Real time: 2.277 s CPU share: 99.55 %</pre>
 
=={{header|Perl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.