-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution34.java
More file actions
39 lines (38 loc) · 1.14 KB
/
Copy pathSolution34.java
File metadata and controls
39 lines (38 loc) · 1.14 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
public class Solution34{
static int factorial(int n){
if(n < 0){
return 0;
}
if(n == 0 || n == 1){
return 1;
}
int[] table = new int[n+1];
table[0] = 1;
for(int i = 1; i <= n; ++i){
table[i] = i * table[i - 1];
}
return table[n];
}
public static void main(String[] args) {
//Upper limit -> factorial(9) * 7, since factorial(9) * 8 yields 2903040, a 7-digit number, implying that a solution of 8 or more digits is impossible.
//Pre-computing factorials from 1 to 9 for speed up.
int[] factorials = new int[10];
for(int i = 0; i < 10;i++){
factorials[i] = factorial(i);
}
int result = 0;
for(int j = 10; j < 2540161; j++){
int factorialSum = 0;
int num = j;
while(num > 0){
int digit = num % 10;
num /= 10;
factorialSum += factorials[digit];
}
if(factorialSum == j){
result += j;
}
}
System.out.println(result);
}
}