Skip to content

Feature/sum of fifth powers #1743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add countDistinctPowers function and associated tests
  • Loading branch information
gaye-lamine committed Oct 21, 2024
commit 02360d3ccacb249227a3c40e037e16cbdc610ad5
33 changes: 33 additions & 0 deletions Project-Euler/Problem030.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Problem - Sum of Fifth Powers of Digits
*
* Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
*
* @see {@link https://projecteuler.net/problem=30}
*/

/**
* Main function to calculate the sum of all numbers that can be expressed
* as the sum of the fifth powers of their digits.
*
* @returns {number} The sum of all numbers that can be written as the sum of fifth powers of their digits.
*/
function sumOfFifthPowers() {
const fifthPowers = Array.from({ length: 10 }, (_, i) => i ** 5)
const results = []

for (let num = 10; num < 354295; num++) {
const sumOfDigits = num
.toString()
.split('')
.reduce((sum, digit) => sum + fifthPowers[digit], 0)

if (sumOfDigits === num) {
results.push(num)
}
}

return results.reduce((acc, curr) => acc + curr, 0)
}

export { sumOfFifthPowers }
8 changes: 8 additions & 0 deletions Project-Euler/test/Problem030.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { sumOfFifthPowers } from '../Problem030' // Adjust the path if necessary

describe('sumOfFifthPowers', () => {
it('should return the sum of all numbers that can be written as the sum of fifth powers of their digits', () => {
const result = sumOfFifthPowers()
expect(result).toBe(443839) // Expected result based on the problem statement
})
})