feat(euler): 16 - Power digit sum
-
This commit is contained in:
parent
be39d481e7
commit
867aa5a6fd
2 changed files with 16 additions and 1 deletions
|
@ -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] [14 - Longest Collatz sequence](src/14%20-%20Longest%20Collatz%20sequence.ts)
|
||||||
- [x] [15 - Lattice paths](src/15%20-%20Lattice%20paths.ts)
|
- [x] [15 - Lattice paths](src/15%20-%20Lattice%20paths.ts)
|
||||||
- [Thoughts](thoughts/15%20-%20Lattice%20paths.md)
|
- [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
|
- [ ] 17 - Number letter counts
|
||||||
- [ ] 18 - Maximum path sum I
|
- [ ] 18 - Maximum path sum I
|
||||||
- [ ] 19 - Counting Sundays
|
- [ ] 19 - Counting Sundays
|
||||||
|
|
15
challenges/euler/src/16 - Power digit sum.ts
Normal file
15
challenges/euler/src/16 - Power digit sum.ts
Normal file
|
@ -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));
|
Loading…
Reference in a new issue