-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblem_074.java
More file actions
54 lines (39 loc) · 1.25 KB
/
Problem_074.java
File metadata and controls
54 lines (39 loc) · 1.25 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
53
54
package euler;
import java.util.ArrayList;
public class Problem_074 {
static int digitFactorialChains() {
return digitFactorialChains(1000000, 60);
}
private static int digitFactorialChains(int thresh, int size) {
int count = 0;
for (int i = 10; i < thresh; i++) {
ArrayList<String> chain = new ArrayList<String>();
chain.add(Integer.toString(i));
String next = digitFact(Integer.toString(i));
while (!chain.contains(next)) {
chain.add(next);
next = digitFact(next);
}
if (chain.size() == size) {
// System.out.println(Arrays.toString(chain.toArray()));
count++;
}
}
return count;
}
private static String digitFact(String x) {
int sum = 0;
for (int i = 0; i < x.length(); i++) {
sum += fact(Integer.parseInt(x.substring(i, i+1)));
}
return Integer.toString(sum);
}
private static int fact(int n) {
int fact = 1;
while (n > 1) {
fact *= n;
n--;
}
return fact;
}
}