jsx代码如下
const [labaNumber, setLabaNumber] = useState();
useEffect(() => {
const findLabaNumber = () => {
let labaNumberArr = [];
//穷举法,从100到999寻找喇叭花数
for (let i = 100; i < 1000; i++) {
//abc分别表示百位,十位,个位
const a = parseInt(i / 100);
const b = parseInt((i - a * 100) / 10);
const c = i - a * 100 - b * 10;
//根据喇叭花数的条件寻找喇叭花数
const total = factorial(a) + factorial(b) + factorial(c);
if (total === i) {
//放进数组
labaNumberArr.push(i);
}
}
setLabaNumber(labaNumberArr);
};
findLabaNumber();
}, []);
console.log('这就是喇叭花数:---', labaNumber);
//累乘器
const factorial = number => {
let total = 1;
for (let i = 1; i <= number; i++) {
total *= i;
}
return total;
};