-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem041.java
More file actions
77 lines (59 loc) · 1.59 KB
/
problem041.java
File metadata and controls
77 lines (59 loc) · 1.59 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package project_beuler;
public class problem041 {
// We shall say that an n-digit number is pandigital if it
// makes use of all the digits 1 to n exactly once.
// For example, 2143 is a 4-digit pandigital and is also prime.
// What is the largest n-digit pandigital prime that exists?
private int panprime = 0;
public problem041() {
}
public int betterPandigital() {
String currNum = "987654321";
while (!currNum.equals("123456789")) {
int num = Integer.parseInt(currNum);
if (Interface.isPrime(num))
return num;
currNum = updateNum(currNum);
}
return 0;
}
// 4321
// 4312
// 4231
// 4213
// 4132
// 4123
private String updateNum(String currNum) {
// int index = 0;
boolean des = true, asc = false;
for(int i = 0; i < currNum.length()-1; i++) {
if(!asc)
des = currNum.charAt(i) > currNum.charAt(i+1);
if(!des)
asc = currNum.charAt(i) < currNum.charAt(i+1);
if(asc == des)
break;//this is bad
}//this means swap from original
return currNum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
problem041 solver = new problem041();
System.out.println(solver.betterPandigital());
// for(int n = 9; n > 0; n--)
// solver.pandigital("", n);
// System.out.println(solver.panprime);
}
public void pandigital(String path, int n) {
if(panprime > 0)
return;
if(path.length() == n) {
if(Interface.isPrime(Integer.parseInt(path)))
panprime = Integer.parseInt(path);
return;
}
for(int dig = n; dig > 0; dig--)
if(path.indexOf(dig+"") == -1)
pandigital(path+dig, n);
}
}