Skip to content

Commit 02360d3

Browse files
committed
Add countDistinctPowers function and associated tests
1 parent fbe4fb4 commit 02360d3

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Project-Euler/Problem030.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Problem - Sum of Fifth Powers of Digits
3+
*
4+
* Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
5+
*
6+
* @see {@link https://projecteuler.net/problem=30}
7+
*/
8+
9+
/**
10+
* Main function to calculate the sum of all numbers that can be expressed
11+
* as the sum of the fifth powers of their digits.
12+
*
13+
* @returns {number} The sum of all numbers that can be written as the sum of fifth powers of their digits.
14+
*/
15+
function sumOfFifthPowers() {
16+
const fifthPowers = Array.from({ length: 10 }, (_, i) => i ** 5)
17+
const results = []
18+
19+
for (let num = 10; num < 354295; num++) {
20+
const sumOfDigits = num
21+
.toString()
22+
.split('')
23+
.reduce((sum, digit) => sum + fifthPowers[digit], 0)
24+
25+
if (sumOfDigits === num) {
26+
results.push(num)
27+
}
28+
}
29+
30+
return results.reduce((acc, curr) => acc + curr, 0)
31+
}
32+
33+
export { sumOfFifthPowers }

Project-Euler/test/Problem030.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { sumOfFifthPowers } from '../Problem030' // Adjust the path if necessary
2+
3+
describe('sumOfFifthPowers', () => {
4+
it('should return the sum of all numbers that can be written as the sum of fifth powers of their digits', () => {
5+
const result = sumOfFifthPowers()
6+
expect(result).toBe(443839) // Expected result based on the problem statement
7+
})
8+
})

0 commit comments

Comments
 (0)