project euler 34
来源:互联网 发布:软件升级服务 增值税 编辑:程序博客网 时间:2024/06/01 10:08
Problem 34
Digit factorials
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.
各位数字的阶乘
145是个有趣的数,因为1! + 4! + 5! = 1 + 24 + 120 = 145。
找出所有各位数字的阶乘和等于其本身的数,并求它们的和。
注意:因为1! = 1和2! = 2不是和的形式,所以它们并不在讨论范围内。
package projecteuler;import java.util.HashSet;import java.util.Set;import org.junit.Test;public class Prj34 {/** * 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. */@Testpublic void test(){System.out.println(Calculator.calculate());}public static class Calculator{public static long calculate(){long val = (long) Math.pow(10, getBits() - 1);Set<Long> set = new HashSet<Long>();for( long i = 3 ; i < val ; i ++){if( checkNum(i)){set.add(i);System.out.println(i);}if( i % 10000 == 0){System.out.println("iter =" + i);}}long sum = 0;for( long val_ : set){sum += val_ ;System.out.println(val_);}System.out.println(sum);return sum;}private static int getBits(){int val = NN.NINE.getVal();int bits = 1;while( val * bits >= Math.pow(10, bits - 1)){bits += 1;}return bits;}private static boolean checkNum(long i) {int sum = 0;int num = 0;long val = i;while( val >= 10){num = Long.valueOf( val % 10).intValue();sum += NN.getNN(num ).getVal();val /= 10;}num = Long.valueOf( val).intValue();sum += NN.getNN(num).getVal();if( sum == i){return true;}else {return false;}}}public static enum NN{ZERO(0),ONE(1),TWO(2 * 1),THREE(3 * 2 * 1),FOUR(4 * 3 * 2 * 1),FIVE(5 * 4 * 3 * 2 * 1),SIX(6 * 5 * 4 * 3 * 2 * 1),SEVEN(7 * 6 * 5 * 4 * 3 * 2 * 1),EIGHT(8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),NINE(9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1);private int val;private NN(int val){if( val == 0){this.val = 1;return;}this.val = val;}public int getVal(){return this.val;}public static NN getNN(int val){switch (val) {case 0: return ZERO;case 1: return ONE;case 2: return TWO;case 3: return THREE;case 4: return FOUR;case 5: return FIVE;case 6: return SIX;case 7: return SEVEN;case 8: return EIGHT;case 9: return NINE;default:return ZERO;}}}}
0 0
- Project Euler - Problem 34
- Project euler 34
- Project Euler problem 34
- Project Euler 34
- project euler Problem 34
- project euler-34
- project euler 34
- Project Euler
- project euler
- Project Euler
- Project Euler:Problem 34 Digit factorials
- Project Euler .net
- Project Euler 0
- Project Euler 1-6
- Project Euler 7-10
- Euler Project Problem 6
- project euler problem 11
- project euler problem14
- Handler源码解析
- Python实例浅谈之三Python与C/C++相互调用
- 计算理论中的莱斯定理(Rice's Theorem)——证明与应用
- 杭电 1003 max sum
- Java中重载和重写的区别
- project euler 34
- Android - OOM
- python输出乱码
- 基础科学(浅谈黑洞I)
- java获取系统时间比实际时间少了8个小时的解决方法
- Java中的拦截器、过滤器、监听器
- project euler 35
- html5中js添加下拉菜单
- OC中的方法