Does my number look big in this?

jenny
Member
Joined: 2025-08-24 15:11:06
2024-06-04 03:48:36

A Narcissistic Number (or Armstrong Number) is a positive number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this Kata, we will restrict ourselves to decimal (base 10).

For example, take 153 (3 digits), which is narcissistic:

` 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
and 1652 (4 digits), which isn't:

```
`1^4 + 6^4 + 5^4 + 2^4 = 1 + 1296 + 625 + 16 = 1938`
```

The Challenge:

Your code must return true or false (not 'true' and 'false') depending upon whether the given number is a Narcissistic number in base 10.

This may be True and False in your language, e.g. PHP.

Error checking for text strings or other invalid inputs is not required, only valid positive non-zero integers will be passed into the function.

Image preview

blessedtechie
Admin
Joined:
2024-06-04 19:51:13

@"dhtml"#p285

Image preview

This took me some time to understand, but I later figured it out. First, convert the number to a string to easily iterate through each digit. Use **`len(num_str)`** to determine the number of digits. Use a generator expression to iterate over each digit, convert it back to an integer, raise it to the power of the total number of digits, and sum these values. Finally, compare the calculated sum to the original number. If they are equal, the number is Narcissistic, otherwise it is not.

jenny
Member
Joined: 2025-08-24 15:11:06
2024-07-25 10:38:19
[[9,31],[29]]
Facebook X (Twitter) Instagram LinkedIn Telegram WhatsApp