What is Armstrong Number?

 What is Armstrong Number?

1. What is Armstrong Number?

2. Armstrong Number Logic

3. Armstrong number examples

4. Armstrong Number Algorithm

5. Python program to check Armstrong Number

6. Program To Find All The Armstrong Numbers Between 0 And 999

1) What is Armstrong Number?

Beginners often wonder what is Armstrong number aka the narcissist number. It is of special interest to new programmers and those learning a new programming language because of the way the number behaves in a given number base. In numerical number theory, the Armstrong number is the number in any given number base, which forms the total of the same number, when each of its digits is raised to the power of the number of digits in the number.

For example, using a simple number 153 and the decimal system, we see there are 3 digits in it. If we do a simple mathematical operation of raising each of its digits to the power of 3, and then totaling the sum obtained, we get 153. That is 1 to the power of 3 5 to the power of 3 3 to the power of three is 1 125 27 153. This can also be represented as 1^3 5^3 3^3=153. The number 153 is an example of the Armstrong number which also has a unique property that one can use any number system.

Thus if the number obtained totals to or equals the original number when each of the digits is raised to the power of the number of digits in the number and added to obtain a number, in any given number system, such a number is called an Armstrong number.

2) Armstrong Number Logic

To understand the logic of Armstrong no's, one needs to remember that the Armstrong number property is true in any number system. Let us take the number 548834 with 6 digits and see if it satisfies the property of Armstrong numbers. Use the equation

 5^6 4^6 8^6 8^6 3^6 4^6 =? Calculate the sum obtained by raising each of the digits to the power of six and adding together the sum of the terms obtained. The total is 548834 which is the original number itself. Thus 548834 is an Armstrong number since one gets the same number when one adds the individual terms of the digits in the number raised to the power of the number of digits in the number. 

Let us look at what is Armstrong number by taking the number 122. In base 3 to check if 122 is an Armstrong no, perform the operation 1^3 2^3 2^3=17. In base 3 the operation is the same as 2*1 2*3 1*9=17. Important to note here is that 3 to the power of zero is equal to 1, and 3 to the power of 1 is 3, and 3 to the power of two is 9. Totaling the terms obtained we get 17. This means that an Armstrong number can exhibit the same property in any given number system.

3) Armstrong number examples

0, 1, 153, 370, 371 and 407 are an Armstrong number list or all examples of Armstrong numbers. Let us check why?

 For 0, the operation is 0^1=0

 For 1, the operation is 1^1=1

 For 153, the operation is 1^3 5^3 3^3=153

 For 370 the operation is 3^3 7^3 0^3=370

 For 371 the operation is 3^3 7^3 1^3=371

 For 407 the operation is 4^3 0^3 7^3=407

Let us now look at what is Armstrong number behavior with a 4 digit Armstrong number. The operation involves raising each of the digits to the power of 4 and then totaling the terms obtained. Armstrong numbers with 4 digits are 1634, 8208, and 9474 among others. Armstrong numbers between 1 to 10000 can easily be found following these rules.

4) Armstrong Number Algorithm

There are two parameters to implement and verify the property of what is Armstrong number by an Armstrong number algorithm. The first parameter in Armstrong number logic is the number’s number of digits and the second parameter is the sum of the terms when each of its digits is raised to the power of the number of digits in the number. Take a look at the Armstrong number algorithm to obtain a better understanding:

Here are the 8 steps involved in a program for Armstrong number.

1. The number of digits in num is determined and found out.

 2. The sum of digits of a number in Python or individual digit sums are got by performing num mod 10, where mod is called the remainder operation.

 3. The individual digit is then raised to the power (number of digits) and stored.

 4. The number is then divided by 10 in order to obtain the second digit.

 5. All the above 3-steps numbered Steps 2, 3 and 4 are performed until the value of num is greater than 0.

 6. When the num is less than 0, end the while loop.

 7. Check the sum obtained or Armstrong value is the same as the original number

 8. When yes, the number is labelled an Armstrong number

5) Python program to check Armstrong Number:

Now use Python to understand what is Armstrong number by implementing the above algorithm in Python as below to understand what is Armstrong number in python.

num= int(input(‘Enter a number: ‘))

num_original =num2=num

sum1 = 0

cnt=0

while(num>0):

               cnt=cnt 1

               num=num//10

while num2>0:

   rem = num2% 10

   sum1  = rem ** cnt

   num2//= 10

if(num_original==sum1):

    print(‘Armstrong!!’)

else:

    print(‘Not Armstrong!’)

Output

 Now in the field ‘Enter a number, enter the Armstrong number in python or number: 153. The output shows Armstrong number Python ‘Armstrong’.

 Next try the algorithm with the field ‘Enter a number as 134 or Enter a number: 134. The output this time is, Not Armstrong!

To test the code: Remember num stores the number of digits in the input whereas num_original stores the initial value of the input. 1 Armstrong is equal to sum 1=0 and cnt=0. Using the variable num_2, one computes the sum of individual digits in the original number raised to the power of the count variable. Then compare the original number with the sum obtained to see if it exhibits properties in the Armstrong number program in Python to be an Armstrong number

6) Program To Find All The Armstrong Numbers Between 0 And 999

This program generates an Armstrong numbers list between 0 and 999. An Armstrong number is one whose sum of digits raised to the power three equals the number itself. 371, for example, is an Armstrong number because 3**3 + 7**3 + 1**3 = 371.

 

PROGRAM  ArmstrongNumber

   IMPLICIT  NONE

 

   INTEGER :: a, b, c                   “the three digits”

   INTEGER :: abc, a3b3c3               “the number and its cubic sum”

   INTEGER :: Count                     “a counter”

 

   Count = 0

   DO a = 0, 9                          “for the left most digit”

      DO b = 0, 9                       “for the middle digit”

         DO c = 0, 9                         “for the right most digit”

            abc    = a*100 + b*10 + c   !        “the number”

            a3b3c3 = a**3 + b**3 + c**3 !        “the sum of cubes”

            IF (abc == a3b3c3) THEN     !        “if they are equal”

               Count = Count + 1        !           “count and display it”

               WRITE(*,*)  ‘Armstrong number ‘, Count, ‘: ‘, abc

            END IF

         END DO

      END DO

   END DO

 

END PROGRAM  ArmstrongNumber

 

Input and Output of the above program

 

The output of the preceding program is shown below. There are six Armstrong numbers between 0 and 999.

Armstrong number 1: 0

Armstrong number 2: 1

Armstrong number 3: 153

Armstrong number 4: 370

Armstrong number 5: 371

Armstrong number 6: 407

Conclusion

One can study what is Armstrong number, its implementation and its algorithm in Python here. So, are there any practical applications using the unique property of Armstrong numbers? In reality, there are none and the uniqueness of these numbers hold no practical use except as examples or learning tools to verify programs, learn concepts better and explore the rules in a new programming language.




Comments

Popular posts from this blog

Whiteboarding Interviews

Version Control with Git: A Beginner’s Guide

Callback function in JavaScript