File tree 2 files changed +41
-0
lines changed
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
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 }
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments