Armstrong Numbers are having the property that an n-digit Armstrong Number will be equal to the sum of the nth powers of its digits. Examples are 0,1,2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, ...
Dr Raveendranathan K C (2021). isarmstrong(n) (https://www.mathworks.com/matlabcentral/fileexchange/48603-isarmstrong-n), MATLAB Central File Exchange. Retrieved .
Neat. You could replace the |while| loop with |sprintf|: this is a more robust way of getting the digits. Also the final |if| statement is totally superfluous, the function really should just return the _logical_ value of |sumd==s|.
Just for fun, here is the same same functionality in just two lines:
>> fun = @(s)sum((s-'0').^numel(s));
>> isarm = @(n)n==fun(sprintf('%lu',n));
A MATLAB function to check whether a given number is an Armstrong Number or not.
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!