-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler24.java
More file actions
45 lines (35 loc) · 819 Bytes
/
euler24.java
File metadata and controls
45 lines (35 loc) · 819 Bytes
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
// This does not print permutations in lexographic order
// I actually solved this problem by hand using combinatorics
public class Permutations {
char[] perm = new char[10];
int count = 0;
public final static int N = 1000000;
String number = "0123456789";
int pos = 0;
public Permutations (String perm) {
this.perm = perm.toCharArray();
permute(0);
}
public void swap(int i, int j) {
char temp = perm[i];
perm[i] = perm[j];
perm[j] = temp;
}
public void permute(int k) {
if(k == perm.length) {
count++;
if(count == N-1) {
System.out.println(perm);
return;
}
}
for(int i = k; i < perm.length; i++) {
swap(k, i);
permute(k+1);
swap(i, k);
}
}
public static void main(String[] args) {
Permutations perm = new Permutations("0123456789");
}
}