Program highlighting language features
A narcissistic number (also known as a plus perfect number, pluperfect digital invariant, or Armstrong number) is a number that is the sum of its own digits each raised to the power of the number of digits. A total of 88 narcissistic numbers exist in base 10. Here are some examples:
- 153 = 13 + 53 + 33
- 370 = 33 + 73 + 03
- 407 = 43 + 03 + 73
- 1,634 = 14 + 64 + 34 + 44
- 9,474 = 94 + 44 + 74 + 44
- 115,132,219,018,763,992,565,095,597,973,971,522,401 = 139 + 139 + 539 + 139 + 339 + 239 + 239 + 139 + 939 + 039 + 139 + 839 + 739 + 639 + 339 + 939 + 939 + 239 + 539 + 639 + 539 + 039 + 939 + 539 + 539 + 939 + 739 + 939 + 739 + 339 + 939 + 739 + 139 + 539 + 239 + 239 + 439 + 039 + 139
As you can see, these numbers can get very large, and determining whether larger numbers are narcissistic numbers can become very time consuming. However, we can use Python to make this task easier. The following algorithmm created by Vanessa Osuka, demostrates Python's intuitive yet powerful structure.
Let's break this down line by line to understand what's going on.
Line 1: The function narcissistic is created to take the given number as the parameter value
Line 2: The variable total is a list container for storing the sum of the operation
Line 3: The variable number stores the length of value. Because len() is an operation that can only work with strings, we must use that to convert value to a string before getting its length
Line 4: The length is then converted back into integer and stored in the variable power
Line 5: This loop converts value into a string in order to separate each digit of the number. it then converts the digits back to integers and stores them in an array called digits
Line 6: This part of the program loops through digits and performs the main operation, which is to multiply each digit with the power
Lines 7-8: The resulting value is stored in product, which is then stored in the list total created earlier. The "append" function is necessary for large numbers so that it just attaches the result of each operation to the last result in the list
Line 9: The variable sumtotal stores the sum of all the values in the list total
Lines 10-13: The program compares the value of sumtotal (the sum of the given number's digits) to the value of value (the given number). If they are the same, then the given number is a narcissistic number, and the program prints an output indicating so. If not, the program will print an output indicating it is not.

Line 1: The function narcissistic is created to take the given number as the parameter value
Line 2: The variable total is a list container for storing the sum of the operation
Line 3: The variable number stores the length of value. Because len() is an operation that can only work with strings, we must use that to convert value to a string before getting its length
Line 4: The length is then converted back into integer and stored in the variable power
Line 5: This loop converts value into a string in order to separate each digit of the number. it then converts the digits back to integers and stores them in an array called digits
Line 6: This part of the program loops through digits and performs the main operation, which is to multiply each digit with the power
Lines 7-8: The resulting value is stored in product, which is then stored in the list total created earlier. The "append" function is necessary for large numbers so that it just attaches the result of each operation to the last result in the list
Line 9: The variable sumtotal stores the sum of all the values in the list total
Lines 10-13: The program compares the value of sumtotal (the sum of the given number's digits) to the value of value (the given number). If they are the same, then the given number is a narcissistic number, and the program prints an output indicating so. If not, the program will print an output indicating it is not.
Your turn!
Use the online compiler below to try the code for yourself.



Very interesting! It is cool to see the power of a language when it performs something such as this.
ReplyDelete