File tree Expand file tree Collapse file tree 2 files changed +7
-17
lines changed Expand file tree Collapse file tree 2 files changed +7
-17
lines changed Original file line number Diff line number Diff line change 13
13
* @param {Number } maxNum
14
14
* @returns {Number } Sum of all amicable numbers under maxNum
15
15
*/
16
- export const sumAmicableNum = ( maxNum ) => {
16
+ export const sumAmicableNums = ( maxNum ) => {
17
17
const amicNums = [ ]
18
18
19
19
for ( let i = 1 ; i < maxNum ; i ++ ) {
@@ -23,23 +23,13 @@ export const sumAmicableNum = (maxNum) => {
23
23
amicNums . push ( i , left )
24
24
}
25
25
}
26
- return amicNums . reduce ( ( a , b ) => {
27
- a += b
28
- return a
29
- } )
26
+ return amicNums . reduce ( ( a , b ) => a + b , 0 )
30
27
}
31
28
32
29
const d = ( num ) => {
33
30
const output = [ ]
34
31
for ( let i = 1 ; i < num ; i ++ ) {
35
- if ( ! ( num % i ) ) { output . push ( i ) }
36
- }
37
- if ( ! output . length ) {
38
- return 0
39
- } else {
40
- return output . reduce ( ( a , b ) => {
41
- a += b
42
- return a
43
- } )
32
+ if ( num % i === 0 ) { output . push ( i ) }
44
33
}
34
+ return output . reduce ( ( a , b ) => a + b , 0 )
45
35
}
Original file line number Diff line number Diff line change 1
- import { sumAmicableNum } from '../Problem021.js'
1
+ import { sumAmicableNums } from '../Problem021.js'
2
2
3
3
describe ( 'Amicable numbers' , ( ) => {
4
4
// Project Euler Condition Check
5
- test ( 'if the number is 10000' , ( ) => {
6
- expect ( sumAmicableNum ( 10000 ) ) . toBe ( 31626 )
5
+ test ( 'if the limit is 10000' , ( ) => {
6
+ expect ( sumAmicableNums ( 10000 ) ) . toBe ( 31626 )
7
7
} )
8
8
} )
You can’t perform that action at this time.
0 commit comments