水仙花数(Narcissistic number)也被称为:超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number)。
水仙花数是指一个 3 位数,每个数字的3次幂之和等于它本身。
例如:371 = 3^3 + 7^3 + 1^3
计算过程(动画):
这里列出更多的水仙花数(中式命名法):
153 = 1^3 + 5^3 + 3^3
370 = 3^3 + 7^3 + 0^3
371 = 3^3 + 7^3 + 1^3
407 = 4^3 + 0^3 + 7^3
1634 = 1^4 + 6^4 + 3^4 + 4^4
8208 = 8^4 + 2^4 + 0^4 + 8^4
9474 = 9^4 + 4^4 + 7^4 + 4^4
54748 = 5^5 + 4^5 + 7^5 + 4^5 + 8^5
92727 = 9^5 + 2^5 + 7^5 + 2^5 + 7^5
93084 = 9^5 + 3^5 + 0^5 + 8^5 + 4^5
548834 = 5^6 + 4^6 + 8^6 + 8^6 + 3^6 + 4^6
1741725 = 1^7 + 7^7 + 4^7 + 1^7 + 7^7 + 2^7 + 5^7
4210818 = 4^7 + 2^7 + 1^7 + 0^7 + 8^7 + 1^7 + 8^7
9800817 = 9^7 + 8^7 + 0^7 + 0^7 + 8^7 + 1^7 + 7^7
9926315 = 9^7 + 9^7 + 2^7 + 6^7 + 3^7 + 1^7 + 5^7
24678050 = 2^8 + 4^8 + 6^8 + 7^8 + 8^8 + 0^8 + 5^8 + 0^8
24678051 = 2^8 + 4^8 + 6^8 + 7^8 + 8^8 + 0^8 + 5^8 + 1^8
88593477 = 8^8 + 8^8 + 5^8 + 9^8 + 3^8 + 4^8 + 7^8 + 7^8
146511208 = 1^9 + 4^9 + 6^9 + 5^9 + 1^9 + 1^9 + 2^9 + 0^9 + 8^9
472335975 = 4^9 + 7^9 + 2^9 + 3^9 + 3^9 + 5^9 + 9^9 + 7^9 + 5^9
534494836 = 5^9 + 3^9 + 4^9 + 4^9 + 9^9 + 4^9 + 8^9 + 3^9 + 6^9
912985153 = 9^9 + 1^9 + 2^9 + 9^9 + 8^9 + 5^9 + 1^9 + 5^9 + 3^9
世事本不美,可苦求十全?
注:可,hé,通“何”。
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
namespace Legalsoft.Truffer.Algorithm
{
/// <summary>
/// 水仙花数(Armstrong Number)的算法
/// </summary>
public static class Armstrong_Number
{
private static int[,] NumberPow = new int[11, 11];
public static void Initialize()
{
for (int i = 0; i <= 10; i++)
{
for (int j = 3; j <= 10; j++)
{
NumberPow[i, j] = (int)Math.Pow(i, j);
}
}
}
public static string Armstrong_Numbers(int n)
{
StringBuilder sb = new StringBuilder();
long nq = (long)Math.Pow(10, n - 1);
long np = (long)Math.Pow(10, n);
for (long i = nq; i < np; i++)
{
char[] vs = (i.ToString()).ToCharArray();
int vk = 0;
for (int j = 0; j < vs.Length; j++)
{
int vj = (vs[j] - '0');
vk += NumberPow[vj, n];
}
if (vk == i)
{
sb.AppendLine("<b>" + i + "</b> = ");
string vu = "";
for (int j = 0; j < vs.Length; j++)
{
vu += vs[j] + "^" + n + " + ";
}
sb.AppendLine(vu.Substring(0, vu.Length - 2).Trim() + "<br>");
}
}
return sb.ToString();
}
}
}