Created
June 18, 2018 14:37
-
-
Save hoc081098/084904e58ff057880c37f0a5c35e9941 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math' show pow; | |
/* | |
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: | |
1634 = 1^4 + 6^4 + 3^4 + 4^4 | |
8208 = 8^4 + 2^4 + 0^4 + 8^4 | |
9474 = 9^4 + 4^4 + 7^4 + 4^4 | |
As 1 = 1^4 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. | |
Result: 443839 | |
*/ | |
/* | |
10^(d - 1) <= d * 9^5 < 10^d | |
=>d = 6 | |
*/ | |
bool valid(n, p) { | |
var sum = 0; | |
for (var t = n; t != 0; sum += p[t % 10], t ~/= 10); | |
return sum == n; | |
} | |
Iterable range(start, end) => | |
new Iterable.generate(end - start, (i) => i + start); | |
solve() { | |
final p = {0: 0}; | |
for (var i = 1; i < 10; i++) { | |
p[i] = pow(i, 5); | |
} | |
return range(2, 6 * p[9] + 1) | |
.where((i) => valid(i, p)) | |
.reduce((a, e) => a + e); | |
} | |
main(List<String> arguments) { | |
print(solve()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment