水仙花数 水仙花数(narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, ppdi)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(armstrong number)的3位数的整数有四个分别是153,370,371,407。
阿姆斯特朗数 如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数。
1741725
4210818
9800817
9926315
24678050
24678051
88593477
146511208
472335975
534494836
912985153
32164049650
32164049651
40028394225
42678290603
44708635679
49388550606
82693916578
94204591914
4338281769391370
4338281769391371
21897142587612075
35641594208964132
35875699062250035
1517841543307505039
3289582984443187032
4498128791164624869
4929273885928088826
128468643043731391252
449177399146038697307
21887696841122916288858
27879694893054074471405
27907865009977052567814
28361281321319229463398
35452590104031691935943
174088005938065293023722
188451485447897896036875
239313664430041569350093
1550475334214501539088894
1553242162893771850669378
3706907995955475988644380
3706907995955475988644381
4422095118095899619457938
121204998563613372405438066
121270696006801314328439376
128851796696487777842012787
174650464499531377631639254
177265453171792792366489765
14607640612971980372614873089
19008174136254279995012734740
19008174136254279995012734741
23866716435523975980390369295
1145037275765491025924292050346
1927890457142960697580636236639
2309092682616190307509695338915
17333509997782249308725103962772
186709961001538790100634132976990
186709961001538790100634132976991
1122763285329372541592822900204593
12639369517103790328947807201478392
12679937780272278566303885594196922
1219167219625434121569735803609966019
12815792078366059955099770545296129367
115132219018763992565095597973971522400
115132219018763992565095597973971522401
c#高效求解,所有水仙数,所有阿姆斯特朗数--时间不超过1分钟,内容来自网上,作了适当修改
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.numerics;
class program
{
private static biginteger[] powerof10;
private static biginteger[,] pretable;
private static biginteger[,] pretable2;
private static int[,] pretable3;
private static int[] selected = new int[10];
private static int length;
private static int count = 0;
public static void main()
{
datetime begin = datetime.now;
for (int i = 1; i < 40; i )
{
initpre(i);
search(9, 0, i);
}
console.writeline(datetime.now - begin);
}
private static void initpre(int n)
{
powerof10 = new biginteger[n 1];
powerof10[0] = 1;
length = n;
for (int i = 1; i <= n; i )
powerof10[i] = powerof10[i - 1] * 10;
pretable = new biginteger[10, n 1];
pretable2 = new biginteger[10, n 1];
pretable3 = new int[10, n 1];
for (int i = 0; i < 10; i )
{
for (int j = 0; j <= n; j )
{
pretable[i, j] = biginteger.pow(i, n) * j;
pretable2[i, j] = powerof10[length - 1] - pretable[i, j];
for (int k = n; k >= 0; k--)
{
if (powerof10[k] < pretable[i, j])
{
pretable3[i, j] = k;
break;
}
}
}
}
}
private static bool precheck(int currentindex, biginteger sum, int remaincount)
{
if (sum < pretable[currentindex, remaincount])
return true;
biginteger max = sum pretable[currentindex, remaincount];
max /= powerof10[pretable3[currentindex, remaincount]];
sum /= powerof10[pretable3[currentindex, remaincount]];
while (max != sum)
{
max /= 10;
sum /= 10;
}
if (max == 0)
return true;
int[] counter = getcounter(max);
for (int i = 9; i > currentindex; i--)
if (counter[i] > selected[i])
return false;
for (int i = 0; i <= currentindex; i )
remaincount -= counter[i];
return remaincount >= 0;
}
private static void search(int currentindex, biginteger sum, int remaincount)
{
if (sum >= powerof10[length])
return;
if (remaincount == 0)
{
if (sum >= powerof10[length - 1] && check(sum))
{
count ;
console.writeline("{0}. {1}", count, sum);
}
return;
}
if (!precheck(currentindex, sum, remaincount))
return;
if (sum < pretable2[currentindex, remaincount])
return;
if (currentindex == 0)
{
selected[0] = remaincount;
search(-1, sum, 0);
}
else
{
for (int i = 0; i <= remaincount; i )
{
selected[currentindex] = i;
search(currentindex - 1, sum pretable[currentindex, i], remaincount - i);
}
}
selected[currentindex] = 0;
}
private static bool check(biginteger sum)
{
int[] counter = getcounter(sum);
for (int i = 0; i < 10; i )
{
if (selected[i] != counter[i])
return false;
}
return true;
}
public static int[] getcounter(biginteger value)
{
int[] counter = new int[10];
char[] sumchar = value.tostring().tochararray();
for (int i = 0; i < sumchar.length; i )
counter[sumchar[i] - '0'] ;
return counter;
}
}