diff --git a/challenges/euler/readme.md b/challenges/euler/readme.md index b23f697..91715b4 100644 --- a/challenges/euler/readme.md +++ b/challenges/euler/readme.md @@ -27,7 +27,7 @@ The source code can be found in the [src](src) directory. My thoughts about some - [x] [14 - Longest Collatz sequence](src/14%20-%20Longest%20Collatz%20sequence.ts) - [x] [15 - Lattice paths](src/15%20-%20Lattice%20paths.ts) - [Thoughts](thoughts/15%20-%20Lattice%20paths.md) -- [ ] 16 - Power digit sum +- [x] [16 - Power digit sum](src/16%20-%20Power%20digit%20sum.ts) - [ ] 17 - Number letter counts - [ ] 18 - Maximum path sum I - [ ] 19 - Counting Sundays diff --git a/challenges/euler/src/16 - Power digit sum.ts b/challenges/euler/src/16 - Power digit sum.ts new file mode 100644 index 0000000..382745a --- /dev/null +++ b/challenges/euler/src/16 - Power digit sum.ts @@ -0,0 +1,15 @@ +export {}; + +const powerDigitSum = (base: number, power: number) => { + const answer = BigInt(base ** power).toString(); + let sum = 0; + + for (let i = 0; i < answer.length; i++) { + const number = parseInt(answer[i]); + sum += number; + } + + return sum; +}; + +console.log(powerDigitSum(2, 1000));