MATLAB decomposing numbers (armstrong numbers)

In summary, the conversation discusses the concept of Armstrong numbers and finding a smart way to decompose numbers in MATLAB to determine if they are Armstrong numbers. The definition of Armstrong numbers is also explained, and an example is given. The conversation also mentions a function written by the speaker to find Armstrong numbers, and provides a list of the first 20 Armstrong numbers. The speaker also gives instructions for running the program.
  • #1
pslarsen
23
1
Hello.
I want to solve the problem with armstrong numbers. Is there a smart way to decompose a number in MATLAB?

E.g. 57483920 = [5 7 4 8 3 9 2 0]

I want to find the numbers which has the following property:

Let k be the number of digits in a number, n, and d1,d2,d3,d4... be the digits of n.
Say n=370. Then k=3 and d1=3, d2=7 and d3=0.
Now 3^3+7^3+0^3=370. So 370 is an Armstrong number.
Armstrong iff n=d1^k+d2^k+d3^k+d4^k+..

Another Armstrong number is 153 because 1^3+5^3+3^3 = 153
 
Physics news on Phys.org
  • #2
Okay, I have written my own function but it can surely be optimized. The first 20 Armstrong numbers are:

1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
54748
92727
93084
548834

Enjoy,
Peter.

PS: To run the programme place both MATLAB-files in the same directory and run faktor2.m
 

Attachments

  • num2vec.m
    308 bytes · Views: 634
  • faktor2.m
    209 bytes · Views: 623
  • #3


Hello, thank you for your question. Yes, there is a smart way to decompose a number in MATLAB. You can use the built-in function "num2str" to convert the number into a string, and then use the function "str2num" to convert the string back into individual digits. You can then use a for loop to raise each digit to the power of k and add them together to check if it equals the original number. Here is an example code:

num = 57483920; % replace with desired number

digits = str2num(num2str(num)); % convert number to string and then to individual digits
k = length(digits); % determine number of digits

sum = 0; % initialize sum variable
for i = 1:k
sum = sum + digits(i)^k; % raise each digit to the power of k and add to sum
end

if sum == num % check if sum equals original number
disp([num ' is an Armstrong number.']); % display result
else
disp([num ' is not an Armstrong number.']); % display result
end

I hope this helps with your problem. Best of luck!
 

1. What is MATLAB decomposing numbers?

MATLAB decomposing numbers, also known as Armstrong numbers, are numbers that can be expressed as the sum of their individual digits raised to a power equal to the number of digits in the original number. For example, 153 is a MATLAB decomposing number because 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.

2. How can I check if a number is a MATLAB decomposing number in MATLAB?

To check if a number is a MATLAB decomposing number in MATLAB, you can use the "isarmstrong" function. This function takes in a number as an input and returns a logical value (1 for true and 0 for false) indicating whether the number is a MATLAB decomposing number or not.

3. Can a negative number be a MATLAB decomposing number?

No, a negative number cannot be a MATLAB decomposing number. This is because the sum of the individual digits raised to a power cannot be negative, and the definition of MATLAB decomposing numbers only applies to positive integers.

4. How can I generate a list of MATLAB decomposing numbers in a given range?

To generate a list of MATLAB decomposing numbers in a given range, you can use a loop and the "isarmstrong" function. The loop will iterate through all the numbers in the given range and check if each number is a MATLAB decomposing number using the "isarmstrong" function. If the function returns a logical value of 1 (true), then the number is a MATLAB decomposing number and can be added to a list.

5. Are MATLAB decomposing numbers useful in any real-world applications?

Yes, MATLAB decomposing numbers have applications in fields such as coding theory and cryptography. They are also used in some computer algorithms for data compression. Additionally, the concept of MATLAB decomposing numbers is a popular topic for recreational mathematics and can be used to create interesting number puzzles or games.

Similar threads

  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Precalculus Mathematics Homework Help
Replies
6
Views
768
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Precalculus Mathematics Homework Help
Replies
1
Views
917
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Precalculus Mathematics Homework Help
Replies
1
Views
715
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Replies
7
Views
2K
Back
Top