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>
 
#define MAX_LEN 1681
typedef long long xint;
 
mpz_t power[10];
#define MAX_LEN 16
xintmpz_t dpowdsum[MAX_LEN + 1][11];
int cnt[10], len;
 
void initcheck_perm(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]);
dpow[p][i] = p ? dpow[p-1][i] * i : 1;
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 narcnarc_(int powerpos, int pos, xint value, xint dsumd)
{
xint i, ds, v, ten;
 
if (!pos) {
check_perm();
printf(" %lld", value);
return;
}
 
do {
ten = dpow[pos - 1][10];
for mpz_add(idsum[pos-1], = (dsum[pos ==], power[d]); i < 10; i++) {
ds = dsum + dpow[power]+cnt[id];
narcnarc_(power, pos - 1, v, dsd);
v = value + i * ten;
--cnt[d];
} while (d--);
}
 
void narc(int n)
if (ds >= v + ten) break;
{
int i, p;
len = n;
for (i = 0; i <= 10; i++)
mpz_ui_pow_ui(power[i], i, n);
 
mpz_init_set_ui(dsum[n], 0);
if (v <= ds + dpow[power][9] * (pos - 1))
 
narc(power, pos - 1, v, ds);
printf("length %lldd:", valuen);
}
narc_(n, 9);
putchar('\n');
}
 
Line 48 ⟶ 66:
{
int i;
init();
 
for (i = 10; i <= 1610; i++) {
printfmpz_init("length %d:", power[i]);
for (pi = 01; pi <= MAX_LEN; pi++) narc(i);
narc(i, i, 0, 0);
putchar('\n');
}
 
return 0;
Line 60 ⟶ 75:
{{out}}
<pre>
length 1: 19 28 37 46 5 64 73 82 91 0
length 2:
length 3: 153 370407 371 407370 153
length 4: 16349474 8208 94741634
length 5: 5474893084 92727 9308454748
length 6: 548834
length 7: 1741725 42108189926315 9800817 99263154210818 1741725
length 8: 2467805088593477 24678051 8859347724678050
length 9: 146511208 472335975912985153 534494836 912985153472335975 146511208
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>
Anonymous user