-
Notifications
You must be signed in to change notification settings - Fork 1
/
p74.vala
63 lines (56 loc) · 1.5 KB
/
p74.vala
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
55
56
57
58
59
60
61
62
63
class DigitFactorialChains : GLib.Object {
public static int[] digitFactorials;
public static int factorial(int n) {
int p = 1;
for (int i = 2; i <= n; i++) {
p *= i;
}
return p;
}
public static int[] precomputeDigitFactorials() {
int[] factorials = new int[10];
for (int i = 0; i < factorials.length; i++) {
factorials[i] = factorial(i);
}
return factorials;
}
public static int sumDigitFactorials(int n) {
int s = 0;
while (n > 0) {
s += digitFactorials[n % 10];
n /= 10;
}
return s;
}
public static bool contains(int n, int[] a) {
foreach (int m in a) {
if (n == m) {
return true;
}
}
return false;
}
public static int digitFactorialChainLength(int n) {
int[] seen = {};
while (!contains(n, seen)) {
seen.resize(seen.length + 1);
seen[seen.length - 1] = n;
n = sumDigitFactorials(n);
}
return seen.length;
}
public static int solve() {
int count = 0;
for (int i = 1; i < 1000000; i++) {
if (digitFactorialChainLength(i) == 60) {
count++;
}
}
return count;
}
public static int main(string[] args) {
digitFactorials = precomputeDigitFactorials();
stdout.printf("%d\n", solve());
return 0;
}
}