[Euler]Problem 34 - Digit factorials
2013-04-16 22:10
387 查看
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.
临界点为 9! * 7 = 2540160
console :
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.
临界点为 9! * 7 = 2540160
public class DigitFactorials { public static void main(String[] args) { long before = System.currentTimeMillis(); new DigitFactorials().calculate(); System.out.println("elapsed time is : " + (System.currentTimeMillis() - before)); } private void calculate() { int number = 3; int sum = 0; int temp = 0; while (number <= 2540160) { int sumOfDigits = 0; temp = number; while (temp > 0) { sumOfDigits += calculateFactorial(temp % 10); temp = temp / 10; } if (sumOfDigits == number) { sum += sumOfDigits; } number++; } System.out.println("sum of all numbers is : " + sum); } private int calculateFactorial(int a) { if (a == 0) { return 1; } return a * calculateFactorial(a - 1); } }
console :
sum of all numbers is : 40730 elapsed time is : 211
相关文章推荐
- (Problem 34)Digit factorials
- (Problem 34)Digit factorials
- Problem 34 Digit factorials (暴力)
- Project Euler:Problem 34 Digit factorials
- (Problem 34)Digit factorials
- project euler 34 Digit factorials
- Project Euler Problem 16 Power digit sum
- Project Euler Problem 20 Factorial digit sum
- project euler Problem 34
- Project Euler Problem 25 1000-digit Fibonacci number
- Project Euler Problem 25 :1000-digit Fibonacci number
- [Euler]Problem 30 - Digit fifth powers
- Matlab 编程 Project Euler Problem 20 Factorial digit sum
- Project Euler Problem 92 Square digit chains
- Project Euler problem 34
- http://projecteuler.net/problem=34 [Answer:40730]
- Project Euler Problem 34 Digit factorials
- Project Euler Problem 81-90
- Project Euler problem 64
- project euler problem 25 大数连加Fibonacci序列