How to sum the proper divisors of a number in Pascal

1 Answer

0 votes
program ProperDivisorsSum;

function SumNumberProperDivisors(num: Cardinal): Cardinal;
var
  i, sum: Cardinal;
begin
  sum := 0;
  for i := 1 to num div 2 do
  begin
    if num mod i = 0 then
    begin
      write(i, ', ');
      sum := sum + i;
    end;
  end;
  SumNumberProperDivisors := sum;
end;

var
  sum: Cardinal;
  
begin
  sum := SumNumberProperDivisors(220);
  
  writeln;
  writeln(sum);
end.




(*
run:
  
1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110, 
2844

*)

 



answered Aug 8 by avibootz
...