From c37b88ff88ad1a9cdb097f7df54db3d06ac4dd68 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:45 +0100 Subject: [PATCH] feat(euler): 30 - digit fifth powers --- challenges/euler/readme.md | 2 +- .../euler/src/30 - Digit fifth powers.ts | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 challenges/euler/src/30 - Digit fifth powers.ts diff --git a/challenges/euler/readme.md b/challenges/euler/readme.md index 514bc38..6e963c6 100644 --- a/challenges/euler/readme.md +++ b/challenges/euler/readme.md @@ -41,7 +41,7 @@ The source code can be found in the [src](src) directory. My thoughts about some - [ ] 27 - Quadratic primes - [x] [28 - Number spiral diagonals](src/28%20-%20Number%20spiral%20diagonals.ts) - [ ] 29 - Distinct powers -- [ ] 30 - Digit fifth powers +- [x] [30 - Digit fifth powers](src/30%20-%20Digit%20fifth%20powers.ts) - [ ] 31 - Coin sums - [ ] 32 - Pandigital products - [ ] 33 - Digit cancelling fractions diff --git a/challenges/euler/src/30 - Digit fifth powers.ts b/challenges/euler/src/30 - Digit fifth powers.ts new file mode 100644 index 0000000..c555a1d --- /dev/null +++ b/challenges/euler/src/30 - Digit fifth powers.ts @@ -0,0 +1,38 @@ +// 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. +export = {}; + +// something times 9^5 = x digit number +// 9^5 = 59049 therefore x >= 5 +// 6 * 9^5 = 354294 + +const powers = []; + +for (let i = 0; i < 10; i++) { + powers.push(i ** 5); +} + +const powerSum = (n: number) => { + let sum = 0; + + while (n > 0) { + sum += powers[n % 10]; + n = (n / 10) | 0; + } + + return sum; +}; + +let sum = 0; + +for (let i = 10; i < 6 * powers[9]; i++) { + if (i === powerSum(i)) sum += i; +} + +// Output +console.log(sum);