PROGRAM FriendlyNumbers(Input,Output);
{Written by Jason J Schwarz in Turbo Pascal v6.0.
Purpose : This program finds all friendly number and perfect
           numbers from 2 to 10000.  Perfect numbers are numbers
           whos factors except themselves add up to the number.
           Friendly numbers are numbers whos factors except them
           self add up to a friend, and the friend's factors except
           for it add up to the original number.}

USES CRT;

VAR
    Numb, Frnd : INTEGER;

FUNCTION Friend(Number : INTEGER) : INTEGER;
VAR
   Total, Count, Top, NumMod : INTEGER;
BEGIN
     Top:=Number;
     Total:=1;
     Count:=2;
     REPEAT
           NumMod:=Number MOD Count;
           IF NumMod=0 THEN BEGIN
                Total:=Total+Count;
                Top:=Number DIV Count;
                IF Top<>Count THEN Total:=Total+Top;
           END;{If NumMod=0}
           Count:=Count+1;
     UNTIL Count>=Top;
     Friend:=Total;
END;{Friend}

BEGIN{FriendlyNumbers}
     CLRSCR;
     GOTOXY(30,1);
     WRITELN(Output,'Friendly Number Finder');
     GOTOXY(1,4);
     FOR Numb:=2 TO 10000 DO BEGIN
          Frnd:=Friend(Numb);
          IF Frnd=Numb THEN BEGIN
             WRITELN(Output,Numb,' is a perfect number.');
          END {Frnd=Numb} ELSE BEGIN{Else}
               IF Numb=Friend(Frnd) THEN
                  WRITELN(Output,Numb,' and ',Frnd,' are friendly numbers.');
          END;{Else}
     END;{Numb:=2 TO 10000}
     GOTOXY(29,24);
     WRITE(Output,'Press <RETURN> to exit');
     READLN(Input);
END.{FriendlyNumbers}
