
% This script prompts the user to enter a number (assumed to be a positive
% integer) and determines whether it is perfect. A perfect number is a
% positive integer that is equal to the sum of its proper positive divisors
% (i.e., all its divisors other than itself). Examples:
% 
% 6 is perfect since 6 = 1 + 2 + 3
% 28 is perfect since 28 = 1 + 2 + 4 + 7 + 14
% 15 is not perfect since 15 does not equal 1 + 3 + 5

number = input('Please enter a number: ');

% For every integer i such that 1 <= i < number, we test whether it is a
% divisor of number; if so we add it to the running total being accumulated
% in the variable sum
sum = 0;
i = 2;
while (i < number)
    if (rem(number, i) == 0)
        sum = sum + i;
    end
    
    i = i + 1;
end

% Test if the sum of the proper divisors equals number
if (sum == number)
    fprintf('%d is perfect\n', number);
else
    fprintf('%d is not perfect\n', number);
end
