Practical numbers: Difference between revisions
Content added Content deleted
(→{{header|Pascal}}: Now without generating sum of allset) |
m (→{{header|alternative}}: only memorizing heigher divisors) |
||
Line 341: | Line 341: | ||
==={{header|alternative}}=== |
==={{header|alternative}}=== |
||
Now without generating sum of allset. |
Now without generating sum of allset. |
||
<lang pascal>program |
<lang pascal>program practicalnumbers2; |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$MODE DELPHI}{$OPTIMIZATION ON,ALL} |
{$MODE DELPHI}{$OPTIMIZATION ON,ALL} |
||
Line 348: | Line 349: | ||
{$ENDIF} |
{$ENDIF} |
||
uses |
uses |
||
SysUtils; |
|||
type |
type |
||
tdivs = record |
tdivs = record |
||
DivsVal: array[0..1024 - 1] of Uint32; |
|||
end; |
|||
var |
var |
||
Divs: tDivs; |
Divs: tDivs; |
||
function CheckIsPractical(var Divs:tdivs;n:Uint32):boolean; |
function CheckIsPractical(var Divs: tdivs; n: Uint32): boolean; |
||
//calc all divisors,calc sum of divisors |
//calc all divisors,calc sum of divisors |
||
var |
var |
||
sum: UInt64; |
|||
⚫ | |||
sum: UInt64; |
|||
quot,ug,sq,de: UInt32; |
|||
Begin |
|||
with Divs do |
|||
begin |
|||
with Divs do |
|||
ug := Low(tdivs.DivsVal); |
|||
og := High(tdivs.DivsVal); |
|||
⚫ | |||
⚫ | |||
begin |
begin |
||
sum := 1; |
|||
ug := Low(tdivs.DivsVal); |
|||
i := 2; |
|||
sq := 4; |
|||
de := 5; |
|||
while sq < n do |
|||
⚫ | |||
quot := n div i; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Inc(i); |
|||
sq += de; |
|||
de := de+2; |
|||
⚫ | |||
if sq = n then |
|||
begin |
|||
if sum + 1 < i then |
|||
EXIT(false); |
|||
DivsVal[ug] := i; |
DivsVal[ug] := i; |
||
Inc(sum, i); |
|||
Inc(ug); |
|||
⚫ | |||
end; |
end; |
||
//check higher |
|||
⚫ | |||
⚫ | |||
begin |
|||
Dec(ug); |
|||
i := DivsVal[ug]; |
|||
if sum+1 < i then |
if sum + 1 < i then |
||
EXIT( |
EXIT(false); |
||
Inc(sum, i); |
|||
if sum >= n - 1 then |
|||
break; |
|||
end; |
end; |
||
// |
end;//with |
||
result := true; |
|||
while og < High(tDivs.DivsVal) do |
|||
end; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end;//with |
|||
⚫ | |||
function isPractical(n:Uint32):boolean; |
function isPractical(n: Uint32): boolean; |
||
begin |
|||
Begin |
|||
if n |
if n in [1,2] then |
||
EXIT(True); |
EXIT(True); |
||
if ODD(n) then |
if ODD(n) then |
||
EXIT( |
EXIT(False); |
||
Result := CheckIsPractical(Divs, n); |
|||
end; |
end; |
||
procedure OutIsPractical(n:nativeInt); |
procedure OutIsPractical(n: nativeInt); |
||
begin |
begin |
||
if isPractical(n) then |
|||
writeln(n,' is practical') |
writeln(n, ' is practical') |
||
else |
else |
||
writeln(n,' is not practical'); |
writeln(n, ' is not practical'); |
||
end; |
end; |
||
const |
const |
||
ColCnt = 10; |
|||
MAX = 333; |
|||
var |
var |
||
T0 : |
T0 : int64; |
||
n,col, |
n, col, Count: NativeInt; |
||
begin |
|||
Begin |
|||
col:=ColCnt; |
col := ColCnt; |
||
Count := 0; |
|||
for n := 1 to MAX do |
|||
if isPractical(n) then |
if isPractical(n) then |
||
begin |
begin |
||
Write(n: 5); |
|||
Inc(Count); |
|||
Dec(col); |
|||
if col = 0 then |
if col = 0 then |
||
begin |
|||
writeln; |
writeln; |
||
col :=ColCnt; |
col := ColCnt; |
||
end; |
end; |
||
end; |
end; |
||
writeln; |
writeln; |
||
writeln('There are ', |
writeln('There are ', Count, ' pratical numbers from 1 to ', MAX); |
||
writeln; |
writeln; |
||
T0 := GetTickCount64; |
|||
OutIsPractical(666); |
OutIsPractical(666); |
||
OutIsPractical(6666); |
OutIsPractical(6666); |
||
Line 453: | Line 460: | ||
OutIsPractical(954432); |
OutIsPractical(954432); |
||
OutIsPractical(720); |
OutIsPractical(720); |
||
OutIsPractical( |
OutIsPractical(5184); |
||
OutIsPractical(1441440); |
OutIsPractical(1441440); |
||
OutIsPractical(99998640); |
OutIsPractical(99998640); |
||
Writeln( (GetTickCount64- T0)/1000:7:5,' s'); |
|||
T0 := GetTickCOunt64; |
|||
⚫ | |||
count := 0; |
|||
⚫ | |||
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}} |
{{out}} |
||
<pre> TIO.RUN |
<pre> TIO.RUN |
||
Line 476: | Line 491: | ||
954432 is not practical |
954432 is not practical |
||
720 is practical |
720 is practical |
||
5184 is practical |
|||
1441440 is practical |
1441440 is practical |
||
99998640 is not 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}}== |
=={{header|Perl}}== |