-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathArmstrongNumbersBruteforce.java
More file actions
50 lines (45 loc) · 1.44 KB
/
ArmstrongNumbersBruteforce.java
File metadata and controls
50 lines (45 loc) · 1.44 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
import java.util.ArrayList;
import java.util.List;
public class ArmstrongNumbersBruteforce {
private static long[][] pows;
private static void genPows(int N) {
if (N > 20) throw new IllegalArgumentException();
pows = new long[10][N + 1];
for (int i = 0; i < pows.length; i++) {
long p = 1;
for (int j = 0; j < pows[i].length; j++) {
pows[i][j] = p;
p *= i;
}
}
}
private static boolean check(long i) {
long powSum = 0;
long x = i;
int N = ((int) Math.log10(x)) + 1;
do {
int mod = (int) x % 10;
powSum += pows[mod][N];
x = x / 10;
} while (x != 0);
return powSum == i;
}
public static List<Long> generate(int maxN) {
if (maxN >= 20) throw new IllegalArgumentException();
List<Long> results = new ArrayList<>();
genPows(maxN);
long limit = (long) Math.pow(10, maxN);
for (long i = 1; i < limit; i++) {
if (check(i)) results.add(i);
}
return results;
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
List<Long> list = generate(9);
long finish = System.currentTimeMillis();
System.out.println("Time consumed: " + (finish - start) + " ms");
System.out.println(list.size());
System.out.println(list);
}
}