Narcissistic decimal number: Difference between revisions

From Rosetta Code
Content added Content deleted
(Perl 6 entry)
mNo edit summary
Line 6: Line 6:
The task is to generate and show here, the first 25 narcissistic numbers.
The task is to generate and show here, the first 25 narcissistic numbers.


=={{header|Perl6}}==
=={{header|Perl 6}}==
Here is a straightforward, naive implementation. Should work but takes ages.
Here is a straightforward, naive implementation. Should work but takes ages.
<lang perl6>sub is-narcissic(Int $n) { $n == ([+] $n.comb)**$n.chars }
<lang perl6>sub is-narcissic(Int $n) { $n == ([+] $n.comb)**$n.chars }


say (grep &is-narcissic, 0 .. *)[^15];</lang>
say (grep &is-narcissic, 0 .. *)[^25];</lang>


=={{header|Python}}==
=={{header|Python}}==

Revision as of 23:11, 6 March 2014

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.

A Narcissistic decimal number is a positice decimal number, Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle n} in which if there are Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle m} digits in the number then the sum of all the individual digits of the number raised to the power Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle m} is equal to Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle n} .

For example, if Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle n} is 153 then Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle m} , the number of digits is 3 and we have Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle 1^3+5^3+3^3 = 1+125+27 = 153} and so 153 is a narcissistic decimal number.

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

Perl 6

Here is a straightforward, naive implementation. Should work but takes ages. <lang perl6>sub is-narcissic(Int $n) { $n == ([+] $n.comb)**$n.chars }

say (grep &is-narcissic, 0 .. *)[^25];</lang>

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