Perfect numbers: Difference between revisions

Line 929:
=={{header|MATLAB}}==
Standard algorithm:
<lang MATLAB>function tfperf = isPerfect(n)
total = 0;
for k = 1:n-1
Line 936:
end
end
tfperf = total == n;
end</lang>
Faster algorithm:
<lang MATLAB>function tfperf = isPerfect(n)
if n < 2
tfperf = false;
else
total = 01;
k = 12;
upperLimitquot = ceil(n/2);
while k <= upperLimitquot && total <= n
if ~mod(n, k)
totalquot = total+n/k;
total = total+k+quot;
end
k = k+1;
end
tfperf = total == n;
end
end</lang>
Anonymous user