[Euler]Problem 30 - Digit fifth powers
2013-04-09 21:58
387 查看
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
As 1 = 14 is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
console:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
As 1 = 14 is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
import java.util.HashMap; import java.util.Map; public class DigitFifthPowers { Map<Integer, Double> map = new HashMap<Integer, Double>(); public static void main(String[] args) { long before = System.currentTimeMillis(); new DigitFifthPowers().calculate(); System.out.println("elapsed time is : " + (System.currentTimeMillis() - before)); } private void calculate() { int count = 0; for (int i = 0; i < 10; i++) { map.put(i, Math.pow(i, 5)); } System.out.print("the numbers that can be written as the sum of fifth powers of their digits is : "); for (int i = 2; i < 1000000; i++) { if (isThisIt(i)) { System.out.print(i + " "); count += i; } } System.out.println(); System.out.println(count); } private boolean isThisIt(int i) { Double sumOfDigits = 0d; int temp = i; while (temp > 0) { sumOfDigits += map.get(temp % 10); temp /= 10; } return sumOfDigits == i; } }
console:
the numbers that can be written as the sum of fifth powers of their digits is : 4150 4151 54748 92727 93084 194979 443839 elapsed time is : 111
相关文章推荐
- Project Euler:Problem 30 Digit fifth powers
- Problem 30 Digit fifth powers (暴力枚举)
- projecteuler---->problem=30----Digit fifth powers
- Problem 30—Digit fifth powers
- Euler: Digit fifth powers
- project euler 30 Digit fifth powers
- [Euler]Problem 34 - Digit factorials
- PE 030 Digit fifth powers
- project euler problem 30
- 欧拉工程第30题:Digit fifth powers
- Digit fifth powers
- Matlab 编程 Project Euler Problem 20 Factorial digit sum
- Project Euler Problem 92 Square digit chains
- Project Euler problem 30
- Project Euler Problem 25 :1000-digit Fibonacci number
- Project Euler Problem 20 Factorial digit sum
- Project Euler Problem 30
- Project Euler Problem 16 Power digit sum
- Project Euler Problem 25 1000-digit Fibonacci number
- Project Euler 题解 #30 Digit fifth powers