feat(euler): 30 - digit fifth powers
This commit is contained in:
parent
de46c70309
commit
c37b88ff88
2 changed files with 39 additions and 1 deletions
|
@ -41,7 +41,7 @@ The source code can be found in the [src](src) directory. My thoughts about some
|
||||||
- [ ] 27 - Quadratic primes
|
- [ ] 27 - Quadratic primes
|
||||||
- [x] [28 - Number spiral diagonals](src/28%20-%20Number%20spiral%20diagonals.ts)
|
- [x] [28 - Number spiral diagonals](src/28%20-%20Number%20spiral%20diagonals.ts)
|
||||||
- [ ] 29 - Distinct powers
|
- [ ] 29 - Distinct powers
|
||||||
- [ ] 30 - Digit fifth powers
|
- [x] [30 - Digit fifth powers](src/30%20-%20Digit%20fifth%20powers.ts)
|
||||||
- [ ] 31 - Coin sums
|
- [ ] 31 - Coin sums
|
||||||
- [ ] 32 - Pandigital products
|
- [ ] 32 - Pandigital products
|
||||||
- [ ] 33 - Digit cancelling fractions
|
- [ ] 33 - Digit cancelling fractions
|
||||||
|
|
38
challenges/euler/src/30 - Digit fifth powers.ts
Normal file
38
challenges/euler/src/30 - Digit fifth powers.ts
Normal file
|
@ -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);
|
Loading…
Reference in a new issue