Anonymous user
Narcissistic decimal number: Difference between revisions
→{{header|C}}: replaced with arbitrary length
m (→{{header|Perl 6}}: invert map to method for readability and end-weight) |
(→{{header|C}}: replaced with arbitrary length) |
||
Line 9:
=={{header|C}}==
It prints the first 25 numbers, though not in order...
<lang c>#include <stdio.h>
#include <gmp.h>
mpz_t power[10];
▲#define MAX_LEN 16
int cnt[10], len;
void
{
char s[MAX_LEN + 1];
int i, p;▼
int i, c, out[10] = { 0 };
for (p = 0; p <= MAX_LEN; p++)▼
for (i = 0; i <= 10; i++)▼
mpz_get_str(s, 10, dsum[0]);
for (i = 0; s[i]; i++) {
c = s[i]-'0';
if (++out[c] > cnt[c]) return;
}▼
if (i == len)
gmp_printf(" %Zd", dsum[0]);
}
void
{
if (!pos) {
check_perm();
printf(" %lld", value);▼
return;
}
do {
--cnt[d];
} while (d--);
void narc(int n)
{
len = n;
mpz_ui_pow_ui(power[i], i, n);
mpz_init_set_ui(dsum[n], 0);
▲ narc(power, pos - 1, v, ds);
▲ }
narc_(n, 9);
}
Line 48 ⟶ 66:
{
int i;
for (i =
▲ putchar('\n');
▲ }
return 0;
Line 60 ⟶ 75:
{{out}}
<pre>
length 1:
length 2:
length 3:
length 4:
length 5:
length 6: 548834
length 7:
length 8:
length 9:
length 10: 4679307774
length 11: 94204591914 82693916578 49388550606 44708635679 42678290603 40028394225 32164049651 32164049650
length 12:
length 13:
length 14: 28116440335967
length 15:
length 16: 4338281769391371 4338281769391370
length 17: 35875699062250035 35641594208964132 21897142587612075
length 18:
^C
</pre>
|