Two integers 



N


{\displaystyle N}

 and 



M


{\displaystyle M}

 are said to be amicable pairs if 



N
≠
M


{\displaystyle N\neq M}

 and the sum of the proper divisors of 



N


{\displaystyle N}

 (




s
u
m

(

p
r
o
p
D
i
v
s

(
N
)
)


{\displaystyle \mathrm {sum} (\mathrm {propDivs} (N))}

) 



=
M


{\displaystyle =M}

 as well as 




s
u
m

(

p
r
o
p
D
i
v
s

(
M
)
)
=
N


{\displaystyle \mathrm {sum} (\mathrm {propDivs} (M))=N}

.



Example

1184 and 1210 are an amicable pair, with proper divisors:

   1, 2, 4, 8, 16, 32, 37, 74, 148, 296, 592   and 
   1, 2, 5, 10, 11, 22, 55, 110, 121, 242, 605   respectively.


Task

Calculate and show here the Amicable pairs below 20,000; (there are eight).



Related tasks
 Proper divisors
 Abundant, deficient and perfect number classifications
 Aliquot sequence classifications and its amicable classification. 


 
begin
 
comment - return n mod m;
integer procedure mod(n, m);
  value n, q; integer n, m;
begin
  mod := n - m * entier(n / m);
end;
 
comment - return sum of the proper divisors of n;
integer procedure sumf(n);
  value n; integer n;
begin
  integer sum, f1, f2;
  sum := 1;
  f1 := 2;
  for f1 := f1 while (f1 * f1) <= n do
    begin
      if mod(n, f1) = 0 then
        begin
           sum := sum + f1;
           f2 := n / f1;
           if f2 > f1 then sum := sum + f2;
        end;
      f1 := f1 + 1;
  end;
  sumf := sum;
end;
 
comment - main program begins here;
integer a, b, found;
outstring(1,"Searching up to 20000 for amicable pairs\n");
found := 0;
for a := 2 step 1 until 20000 do
  begin
    b := sumf(a);
    if b > a then
       begin
         if a = sumf(b) then
            begin
              found := found + 1;
              outinteger(1,a);
              outinteger(1,b);
              outstring(1,"\n");
            end;
       end;
  end;
outinteger(1,found);
outstring(1,"pairs were found");
 
end
 