Narcissistic decimal number: Difference between revisions
m (sp.) |
|||
Line 5: | Line 5: | ||
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|C}}== |
|||
<lang c>#include <stdio.h> |
|||
typedef long long xint; |
|||
#define MAX_LEN 16 |
|||
xint dpow[MAX_LEN + 1][11]; |
|||
void init(void) |
|||
{ |
|||
int i, p; |
|||
for (p = 0; p <= MAX_LEN; p++) |
|||
for (i = 0; i <= 10; i++) |
|||
dpow[p][i] = p ? dpow[p-1][i] * i : 1; |
|||
} |
|||
void narc(int power, int pos, xint value, xint dsum) |
|||
{ |
|||
xint i, ds, v, ten; |
|||
if (!pos) { |
|||
if (value == dsum) |
|||
printf(" %lld", value); |
|||
return; |
|||
} |
|||
ten = dpow[pos - 1][10]; |
|||
for (i = (pos == power); i < 10; i++) { |
|||
ds = dsum + dpow[power][i]; |
|||
v = value + i * ten; |
|||
if (ds >= v + ten) break; |
|||
if (v <= ds + dpow[power][9] * (pos - 1)) |
|||
narc(power, pos - 1, v, ds); |
|||
} |
|||
} |
|||
int main(void) |
|||
{ |
|||
int i; |
|||
init(); |
|||
for (i = 1; i <= 16; i++) { |
|||
printf("length %d:", i); |
|||
narc(i, i, 0, 0); |
|||
putchar('\n'); |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
length 1: 1 2 3 4 5 6 7 8 9 |
|||
length 2: |
|||
length 3: 153 370 371 407 |
|||
length 4: 1634 8208 9474 |
|||
length 5: 54748 92727 93084 |
|||
length 6: 548834 |
|||
length 7: 1741725 4210818 9800817 9926315 |
|||
length 8: 24678050 24678051 88593477 |
|||
length 9: 146511208 472335975 534494836 912985153 |
|||
^C |
|||
</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
Revision as of 05:07, 7 March 2014
A Narcissistic decimal number is a positive decimal number, in which if there are digits in the number then the sum of all the individual digits of the number raised to the power is equal to .
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.
C
<lang c>#include <stdio.h>
typedef long long xint;
- define MAX_LEN 16
xint dpow[MAX_LEN + 1][11];
void init(void) { int i, p; for (p = 0; p <= MAX_LEN; p++) for (i = 0; i <= 10; i++) dpow[p][i] = p ? dpow[p-1][i] * i : 1; }
void narc(int power, int pos, xint value, xint dsum) { xint i, ds, v, ten;
if (!pos) { if (value == dsum) printf(" %lld", value); return; }
ten = dpow[pos - 1][10]; for (i = (pos == power); i < 10; i++) { ds = dsum + dpow[power][i]; v = value + i * ten;
if (ds >= v + ten) break;
if (v <= ds + dpow[power][9] * (pos - 1)) narc(power, pos - 1, v, ds); } }
int main(void) { int i; init();
for (i = 1; i <= 16; i++) { printf("length %d:", i); narc(i, i, 0, 0); putchar('\n'); }
return 0; }</lang>
- Output:
length 1: 1 2 3 4 5 6 7 8 9 length 2: length 3: 153 370 371 407 length 4: 1634 8208 9474 length 5: 54748 92727 93084 length 6: 548834 length 7: 1741725 4210818 9800817 9926315 length 8: 24678050 24678051 88593477 length 9: 146511208 472335975 534494836 912985153 ^C
Perl 6
Here is a straightforward, naive implementation. It works but takes ages. <lang perl6>sub is-narcissistic(Int $n) { $n == [+] $n.comb »**» $n.chars }
for 0 .. * {
if .&is-narcissistic {
.say; last if ++state$ >= 25;
}
}</lang>
- Output:
0 1 2 3 4 5 6 7 8 9 153 370 371 407 Ctrl-C
Here the program was interrupted but if you're patient enough you'll see all the 25 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