# 5 Best Ways to Check Whether a Given Number is a Narcissistic Number in Python

Rate this post

π‘ Problem Formulation: A Narcissistic number is a number that is the sum of its own digits each raised to the power of the number of digits. For instance, 153 is a Narcissistic number because 1^3 + 5^3 + 3^3 = 153. This article provides programs in Python to determine whether a given number is a Narcissistic number, explaining various methods and their implementations.

## Method 1: Using Iteration

This method involves iterating through each digit of the number, raising it to the power of the number of digits, and summing these values to check against the original number. It’s a straightforward method and easy to understand for beginners.

Here’s an example:

```def is_narcissistic(num):
num_str = str(num)
length = len(num_str)
sum_of_powers = sum(int(char) ** length for char in num_str)
return sum_of_powers == num

print(is_narcissistic(153))```

Output: `True`

This code snippet defines a function `is_narcissistic()` that accepts a number as input, converts it to a string to loop through each digit, and calculates the sum of each digit raised to the power of the length of the number. It then returns whether this sum equals the original number.

## Method 2: Using Map Function

As an alternative to explicit iteration, this method takes advantage of the built-in `map()` function, paired with lambda expressions, to raise each digit to the appropriate power and sum them up. This is concise and leverages Python’s functional programming features.

Here’s an example:

```def is_narcissistic(num):
digits = list(map(int, str(num)))
length = len(digits)
return num == sum(map(lambda x: x ** length, digits))

print(is_narcissistic(370))```

Output: `True`

This snippet uses `map()` to convert each character in the number string into an integer. Then it uses another `map()` with a lambda function to raise each digit to the power of the number of digits. The sum of these powers is compared to the original number to determine if it is Narcissistic.

## Method 3: Using List Comprehension

The third method utilizes Python’s list comprehensions for a more pythonic and compact approach. The code is not only elegant but also clear in its intention and supports readability.

Here’s an example:

```def is_narcissistic(num):
digits = [int(d) for d in str(num)]
return num == sum(d ** len(digits) for d in digits)

print(is_narcissistic(1634))```

Output: `True`

This implementation uses list comprehension to create a list of digits from the number. It then compares the original number with the sum of these digits each raised to the power equal to the number of digits using a generator expression inside `sum()`.

## Method 4: Using Recursion

Recursion provides an elegant way of breaking down the problem. This method uses recursive calls to split the number digit by digit, thereby avoiding explicit loops or list conversions.

Here’s an example:

```def is_narcissistic(num, length=None):
if length is None:
length = len(str(num))
if num < 10:
return num ** length
else:
return (num % 10) ** length + is_narcissistic(num // 10, length)

num = 9474
print(num == is_narcissistic(num))```

Output: `True`

This recursive function takes the number and optionally takes its length. It sums the last digits raised to the power of length and recursively calls itself with the number excluding the last digit until the base condition is met (a single digit is left).

## Bonus One-Liner Method 5: Using the Reduce Function

This one-liner uses the `reduce()` function from `functools` to condense the iterative computation into a single line. This method is for those who prefer a functional programming style and concise code, albeit at the expense of readability for some.

Here’s an example:

```from functools import reduce

def is_narcissistic(num):
return num == reduce(lambda x, y: x + y**len(str(num)), [int(i) for i in str(num)], 0)

print(is_narcissistic(8208))```

Output: `True`

The function utilizes `reduce()` with a lambda to sum up each digit in the number raised to the number of digits. The list of digits is generated within the function call itself, making it a compact one-liner.

## Summary/Discussion

• Method 1: Iteration. Beginner-friendly and straightforward. May be slower for very large numbers.
• Method 2: Map Function. Concise and Pythonic. Introduces functional programming but may be less readable to some.
• Method 3: List Comprehension. Elegant and readable. Blends well with Python’s idioms, but performance is similar to iteration.
• Method 4: Recursion. A neat and orderly way of approaching the problem. May hit recursion limits for very large numbers.
• Method 5: Reduce Function. A functional one-liner ideal for succinctness. Potentially harder to understand for those less familiar with functional programming concepts.