uses wincrt;

function isprime(n:longint):boolean;
  var counter:longint;
  begin
    isprime := (n > 1);
    for counter := 2 to trunc(sqrt(n)) do
      if (n mod counter = 0) then
        begin
          isprime := false;
          exit;
        end;
  end;

function isperfect(n:longint):boolean;
  var i,sum:longint;
  begin
    sum := 0;
    for i := 1 to n-1 do
      if (n mod i = 0) then inc(sum,i);
    isperfect := (sum=n);
  end;

function pwr10(x:integer):longint;
  var i:integer; return:longint;
  begin
    return := 1;
    for i := 1 to x do
      return := return * 10;
    pwr10 := return;
  end;

function isarmstrong(n:longint):boolean;
  var number,sum,digit,nodigits:integer;
  begin
    number := n;
    sum := 0;
    while number>0 do
      begin
        nodigits := trunc(ln(number)/ln(10));
        digit := trunc(number/pwr10(nodigits));
        dec(number,digit*pwr10(nodigits));
        inc(sum,digit*digit*digit);
      end;
    isarmstrong := (sum=n);
  end;

var n1,n2,i,sum:longint;

begin
  writeln('This program will sum all armstrong, perfect, and prime numbers within a range');
  writeln;
  write('Enter starting point:    '); readln(n1);
  write('Enter ending point:      '); readln(n2);

  sum := 0;

  for i := n1 to n2 do
    if isprime(i) or isarmstrong(i) or isperfect(i) then inc(sum,i);

  writeln;
  writeln('The sum of these wonderful numbers is: ',sum);
end.
