Amicable pairs: Difference between revisions

m
→‎Direct approach: compiler switch for freepascal, put filehandling after summation , stop showing overflow
m (→‎Direct approach: compiler switch for freepascal, put filehandling after summation , stop showing overflow)
Line 423:
=={{header|Pascal}}==
===Direct approach===
{{works with|Turbo Pascal}}{{works with| Free Pascal}}
This version mutates the Sieve of Eratoshenes from striking out factors into summing factors. The Pascal source compiles with Turbo Pascal (7, patched to avoid the zero divide problem for cpu speeds better than ~150MHz) except that the array limit is too large: 15,000 works but does not reach 20,000. The Free Pascal compiler however can handle an array of 20,000 elements. Because the sum of factors of N can exceed N an ad-hoc SumF procedure is provided, thus the search could continue past the table limit, but at a cost in calculation time.
 
Line 444 ⟶ 445:
Source file:<lang pascal>
Program SumOfFactors; uses crt; {Perpetrated by R.N.McLean, December MCMXCV}
//{$DEFINE ShowOverflow}
{$IFDEF FPC}
{$MODE DELPHI}//tested with lots = 524*1000*1000 takes 75 secs generating KnownSum
{$ENDIF}
var outf: text;
const Limit = 2147483647;
Line 480 ⟶ 485:
ClrScr;
WriteLn('Chasing Chains of Sums of Factors of Numbers.');
Assign(outf,'Factors.txt'); ReWrite(Outf);
WriteLn(Outf,'Chasing Chains of Sums of Factors of Numbers.');
for i:=1 to lots do KnownSum[i]:=1; {Sigh. KnownSum:=1;}
 
{start summing every divisor }
for i:=2 to lots do
begin
j:=i + i;
While j <= lots do {Sigh. For j:=i + i:Lots:i do KnownSum[j]:=KnownSum[j] + i;}
begin
KnownSum[j]:=KnownSum[j] + i;
j:=j + i;
end;
end;
 
{Enough preparation.}
Assign(outf,'Factors.txt'); ReWrite(Outf);
WriteLn(Outf,'Chasing Chains of Sums of Factors of Numbers.');
 
for i:=2 to lots do {Search.}
begin
Line 504 ⟶ 513:
end;
if l >= enuff then writeln('Rope ran out! ',i);
{$IFDEF ShowOverflow}
if sf < 0 then writeln('Overflow with ',i);
{$ENDIF}
if i = sf then {A loop?}
begin {Yes. Reveal its members.}
Anonymous user