Guest User

Untitled

a guest
Jun 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import 'dart:math' show pow;
  2.  
  3. /*
  4. Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
  5.  
  6. 1634 = 1^4 + 6^4 + 3^4 + 4^4
  7. 8208 = 8^4 + 2^4 + 0^4 + 8^4
  8. 9474 = 9^4 + 4^4 + 7^4 + 4^4
  9. As 1 = 1^4 is not a sum it is not included.
  10.  
  11. The sum of these numbers is 1634 + 8208 + 9474 = 19316.
  12.  
  13. Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
  14.  
  15. Result: 443839
  16. */
  17.  
  18. /*
  19. 10^(d - 1) <= d * 9^5 < 10^d
  20. =>d = 6
  21. */
  22.  
  23. bool valid(n, p) {
  24. var sum = 0;
  25. for (var t = n; t != 0; sum += p[t % 10], t ~/= 10);
  26. return sum == n;
  27. }
  28.  
  29. Iterable range(start, end) =>
  30. new Iterable.generate(end - start, (i) => i + start);
  31.  
  32. solve() {
  33. final p = {0: 0};
  34. for (var i = 1; i < 10; i++) {
  35. p[i] = pow(i, 5);
  36. }
  37.  
  38. return range(2, 6 * p[9] + 1)
  39. .where((i) => valid(i, p))
  40. .reduce((a, e) => a + e);
  41. }
  42.  
  43. main(List<String> arguments) {
  44. print(solve());
  45. }
Add Comment
Please, Sign In to add comment