Pascal: Perfect Numbers

program perfect_numbers;

var   limit, sum, i, j : integer

begin
  writeln ('Enter a number as the limit in a search for perfect numbers');
  readln (limit);
  for i:=1 to limit do
    begin
      sum := 0;
      for j := 1 to i div 2 do
        if (i mod j = 0) then sum := sum + j;
      if (sum = i) then writeln (i, ' is perfect');
    end;
  readln;
end.

Much the same as the factors exercise except that we need a sum of the factors. The factors include 1 but not the number itself (obviously!). Is it worth testing odd numbers? 

Back to questions