In: Computer Science
% vector containing perfect numbers
perfect_numbers = [];
% read n from user
n = input("Enter n: ");
current=2;
% while loop
while n>0
is_perfect=true;
total = 0;
% loop from 2 to current and compute sum of factors
for j=1:current-1
% if j is factor of current
if rem(current,j)==0
total = total + j;
end
end
% if total is equal to current number
if total==current
% add this number to perfect
perfect_numbers=[perfect_numbers,current];
% decrement n
n = n-1;
end
current = current + 1;
end
perfect_numbers
.
Output:
.