水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
计算100-1000内的水仙花数:
function test() {
var count = 0;
for (var i = 100; i <= 1000; i++) {
var a = parseInt(i / 100);
var b = parseInt(i % 100 / 10);
var c = parseInt(i % 10);
if (a * a * a + b * b * b + c * c * c == i) {
count++;
console.log(i);
}
}
console.log("count:" + count);
}
test();
本文介绍了水仙花数的概念及其特点,并提供了一段JavaScript代码来找出100到1000之间的所有水仙花数。水仙花数是一种特殊的数字,其特性在于该数的每一位数字的三次方和等于该数本身。
1427

被折叠的 条评论
为什么被折叠?



