-
Notifications
You must be signed in to change notification settings - Fork 32
/
Main.java
48 lines (43 loc) · 995 Bytes
/
Main.java
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
public class Main
{
private final static int LIMIT = 440_000_000;
private final static int[] cache = getCache();
private static int[] getCache()
{
int[] cache = new int[10];
cache[0] = 0;
for (int i = 1; i <= 9; ++i) {
cache[i] = (int)Math.pow(i, i);
}
return cache;
}
private boolean isMunchausen(final int number)
{
int n = number;
int total = 0;
while (n > 0)
{
final int digit = n % 10;
total += cache[digit];
if (total > number) {
return false;
}
n = n / 10;
}
return total == number;
}
private void start()
{
for (int i = 0; i < LIMIT; ++i)
{
if (isMunchausen(i)) {
System.out.println(i);
}
}
}
public static void main(String[] args)
{
Main m = new Main();
m.start();
}
}