欧拉计划34题
2016-03-16 23:38
239 查看
/**
* Digit factorials
* 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.
*
* @author dcb
*
*/
求所有各个位数阶乘和等于本身的数的和
分析:
题目不能,使用循环校验就行,难点在于划分范围
一个10进制数每个位上的最大值为9
而 9!= 362880 共计6位数
9999999 各个位数上的阶乘和位 362880*7=2540160
因此只需循环至2540160就行
代码实现
int result = 0;
for (int i = 10; i < 2540160; i++){
String num = "" + i;
int temp = 0;
for (int j = 0; j < num.length(); j++){
int a = new Integer("" + num.charAt(j));
temp += Utils.factorial(a);
}
if (i == temp){
System.out.println(i);
result += i;
}
}
结果:40730
令人惊讶的是
满足条件的数字只有两个
145
40585
* Digit factorials
* 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.
*
* @author dcb
*
*/
求所有各个位数阶乘和等于本身的数的和
分析:
题目不能,使用循环校验就行,难点在于划分范围
一个10进制数每个位上的最大值为9
而 9!= 362880 共计6位数
9999999 各个位数上的阶乘和位 362880*7=2540160
因此只需循环至2540160就行
代码实现
int result = 0;
for (int i = 10; i < 2540160; i++){
String num = "" + i;
int temp = 0;
for (int j = 0; j < num.length(); j++){
int a = new Integer("" + num.charAt(j));
temp += Utils.factorial(a);
}
if (i == temp){
System.out.println(i);
result += i;
}
}
结果:40730
令人惊讶的是
满足条件的数字只有两个
145
40585
相关文章推荐
- 欧拉计划34题,0的阶乘原来等于1,文盲了。
- 欧拉计划 34
- □ 影片名:《唐山大兄》(34)
- chr(9)、chr(10)、chr(13)、chr(32)、chr(34)讲解
- [转载] LCC编译器的源程序分析(34)continue语句
- Ajax Control Toolkit 34个控件(三 3-34)
- 地磅称量系统之(30~34)从我定义的界面模板中继承窗体并且完善它的的界面编辑控件布局展示主界面的运行效果图
- Windows API一日一练(34)GetSysColor函数
- 求1,1,2,3,5,8,13,21,34...的第N位結果For ASP
- 用递归算法实现1、1、2、3、5、8、13、21、34......
- 传智播客Hibernate视频教程学习笔记34
- the Secret DAILY TEACHINGS-Day 34
- 《BREW进阶与精通――3G移动增值业务的运营、定制与开发》连载之34---手机上应用的预安装
- 【软件测试自动化-QTP系列讲座 34】== 访问对象自身接口的特殊渠道 ==
- 一列数的规则如下:1,1,2,3,5,8,13,21,34.....用递归算法求第30位数是多少
- CodeForces Round#34 ABCD解题报告
- 学好计算机的八条建议——计算机达人成长之路(34)
- 条款34: 将文件间的编译依赖性降至最低 (转自effective c++ second edition)
- 2011-7-31 9:28:34
- 34、linux shell,常用函数strace