-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq32.java
More file actions
63 lines (48 loc) · 1.25 KB
/
q32.java
File metadata and controls
63 lines (48 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
55
56
57
58
59
60
61
62
63
import java.util.*;
public class q32 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
Hashtable<Integer, Boolean> ht = new Hashtable<Integer, Boolean>();
int[] temp = new int[3];
for(int i = 1; i < 1000; ++i){
for(int j = 1; j < 20000/i; ++j){
temp[0] = i; temp[1] = j; temp[2] = i * j;
if(isPandigital(temp) && !ht.containsKey(temp[2])){
System.out.println("The sequence is pandigital: " + temp[0] + " " + temp[1] + " " + temp[2]);
sum += temp[2];
ht.put(temp[2], true);
}
}
}
System.out.println(sum);
}
public static boolean isPandigital(int[] i){
int digits = numDigits(i[0]) + numDigits(i[1]) + numDigits(i[2]);
if(digits != 9) return false;
StringBuffer s = new StringBuffer();
s.append(i[0]); s.append(i[1]); s.append(i[2]);
String test = s.toString();
for(int j = 1; j < digits + 1; ++j){
if(!test.contains(Integer.toString(j))) return false;
}
return true;
}
public static void printArray(int a[]){
int j = 0;
for(int i = 0; i < a.length; ++i){
j *= 10;
j += a[i];
}
System.out.println(j);
return;
}
public static int numDigits(int i){
int a = 0;
while(i > 0){
++a;
i /= 10;
}
return a;
}
}