How to print Narcissistic(Armstrong) Numbers with Python?

PythonProgrammingServer Side Programming

To print Narcissistic Numbers, let's first look at the definition of it. It is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 1, 153, 370 are all Narcissistic numbers. You can print these numbers by running the following code

def print_narcissistic_nums(start, end):
for i in range(start, end + 1):
   # Get the digits from the number in a list:
   digits = list(map(int, str(i)))
   total = 0
   length = len(digits)
   for d in digits:
      total += d ** length
   if total == i:
      print(i)
print_narcissistic_nums(1, 380)

This will give the output

1
2
3
4
5
6
7
8
9
153
370
371
raja
Published on 20-Apr-2018 08:44:10
Advertisements