matlab 欧拉计划 34
程序员文章站
2022-03-31 20:46:22
...
problem 34
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
思路
1.要求:找到满足如下条件的数:位数大于1位;各位数字阶乘相加等于本身。并求出所 有满足该条件的数之和;
2.首先需要确定范围,按9!来估算:9!×8=2903040,结果为一7位数,最大的8位数字阶乘和都达不到8位数,因此范围肯定小于8位数;9!×7=2540160,结果位数相符,因此可以将范围定为[10,2540160].(就是运行时间有点长)
程序
clc;clear all;
t=[];
for i=10:2540160
t1=0;
y=num2str(i);
for j=1:length(y)
t1=t1+factorial(str2num(y(j)));
end
if t1==i
t=[t i];
end
end
上一篇: matlab 欧拉计划 35