-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigit-Factorials.js
More file actions
52 lines (44 loc) · 1.27 KB
/
Digit-Factorials.js
File metadata and controls
52 lines (44 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Description:
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the numbers and the sum of the numbers which are equal to the sum of the factorial of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
digitFactorial() should return an object.
digitFactorial() should return { sum: 40730, numbers: [145, 40585] }
*/
function digits(number){
const digits = []
const string = number.toString()
for(let i=0; i<string.length;i++){
digits.push(parseInt(string.charAt(i)))
}
return digits
}
function fact(num){
var number = 1
for(let i=num; i>=1; i--){
number *= i
}
return number
}
function digitFactorial() {
var sum = 0;
var numbers = [];
for(let i=10;i<=100000;i++){
let temp = []
let prod = digits(i)
for(let j=0;j<prod.length;j++){
temp.push(fact(prod[j]))
}
let dummy = temp.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
//console.log(dummy)
if(dummy==i){
//console.log(dummy)
numbers.push(i)
}
}
//console.log(numbers)
sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
return { sum, numbers };
}
digitFactorial();