Narcissistic decimal number

Revision as of 21:36, 6 March 2014 by rosettacode>Paddy3118 (New draft task and Python solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A Narcissistic decimal number is a positice decimal number, in which if there are digits in the sumber then the sum of all the individual digits of the number raised to the power is equal to .

Narcissistic decimal number is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

For example, if is 153 then , the number of digits is 3 and we have and so 153 is a narcissistic decimal number.

The task is to generate and show here, the first 25 narcissistic numbers.

Python

This solution pre-computes the powers once.

<lang python>from __future__ import print_function from itertools import count, islice

def narcissists():

   for digits in count(0):
       digitpowers = [i**digits for i in range(10)]
       for n in range(int(10**(digits-1)), 10**digits):
           div, digitpsum = n, 0
           while div:
               div, mod = divmod(div, 10)
               digitpsum += digitpowers[mod]
           if n == digitpsum:
               yield n

for i, n in enumerate(islice(narcissists(), 25), 1):

   print(n, end=' ')
   if i % 5 == 0: print() 

print()</lang>

Output:
0 1 2 3 4 
5 6 7 8 9 
153 370 371 407 1634 
8208 9474 54748 92727 93084 
548834 1741725 4210818 9800817 9926315