creating a perfect number function
Show older comments
Hi. I am currently having a prob. I need to create a function that takes in the number(n) as argument and returns true if n is a perfect number. I am not allowed to use vectors and for loop for this question. I am stuck at this few codes:
function num = perfect(n)
i=1;
sum = 0;
while i <= n/2
if rem(n,i) == 0
else
i = i +1
end
sum = sum + i;
end
if n == sum
n = true
I am a beginner in Matlab and I require assistance. Any kind soul out there to help me? Thanks!
1 Comment
John D'Errico
on 30 Aug 2016
Using the name sum as a variable name will create problems down the road for you. Don't name variables the same as useful functions that you will later need. Or expect to see lots of bugs in your code. Of course, you might like buggy code. Your choice.
Accepted Answer
More Answers (1)
alex usefi
on 10 Sep 2018
0 votes
How to get numbers like this from the code? is_perfect(6)
show result code: 1 2 3
1 Comment
Please start your own question rather than highjacking the answer box of an another question for something that clearly is not an answer.
Note that I would expect that a function called is_perfect to return either true or false, not a vector of numbers.
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!