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 .
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
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));
>> isarm(0)
ans =
1
>> isarm(1)
ans =
1
>> isarm(153)
ans =
1
>> isarm(154)
ans =
0
A MATLAB function to check whether a given number is an Armstrong Number or not.