-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem09.java
More file actions
113 lines (93 loc) · 3.8 KB
/
problem09.java
File metadata and controls
113 lines (93 loc) · 3.8 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2.
There is exactly one Pyth triplet for which a+b+c = 1000. Find product abc.
*/
// TODO: As a recursion, we are checking numbers > 1000 so doing more branching than we need to
// We stop the loop if the sum > 1000 but still do the transformations
import java.util.Arrays;
public class problem09 {
public static void main(String[] args) {
int[] triple = { 3, 4, 5 };
boolean foundIt = false;
checkTriple(triple);
System.out.println("Didn't find anything, trying t1...");
checkTriple(transform1(triple));
System.out.println("Didn't find anything, trying t2...");
checkTriple(transform2(triple));
System.out.println("Didn't find anything, trying t3...");
checkTriple(transform3(triple));
}
public static int[] transform1(int[] triple) {
int[] newtriple = {
triple[0] - 2*triple[1] + 2*triple[2],
2*triple[0] - triple[1] + 2*triple[2],
2*triple[0] - 2*triple[1] + 3*triple[2]
};
return newtriple;
}
public static int[] transform2(int[] triple) {
int[] newtriple = {
triple[0] + 2*triple[1] + 2*triple[2],
2*triple[0] + triple[1] + 2*triple[2],
2*triple[0] + 2*triple[1] + 3*triple[2]
};
return newtriple;
}
public static int[] transform3(int[] triple) {
int[] newtriple = {
-triple[0] + 2*triple[1] + 2*triple[2],
-2*triple[0] + triple[1] + 2*triple[2],
-2*triple[0] + 2*triple[1] + 3*triple[2]
};
return newtriple;
}
public static boolean doesTripleSumTo1000(int[] triple) {
int sum = triple[0]+triple[1]+triple[2];
if (sum == 1000) {
System.out.println(triple[0]*triple[1]*triple[2]); // if it sums to 1000, we want the product
// let's throw in which triple did it, too
System.out.println(Arrays.toString(triple));
return true;
}
else {
return false;
}
}
public static boolean checkTriple(int[] triple) {
boolean found = false;
int[] newtriple = Arrays.copyOf(triple, triple.length);
int coef = 1;
//System.out.println("Incoming triple is: " + Arrays.toString(triple));
while (newtriple[0]+newtriple[1]+newtriple[2] <= 1000) {
if (newtriple[0] + newtriple[1] + newtriple[2] == 1000) {
found = true;
//System.out.println(Arrays.toString(newtriple));
coef++;
}
else {
//System.out.println("The coefficient is currently: " + coef);
newtriple[0] = triple[0]*coef;
newtriple[1] = triple[1]*coef;
newtriple[2] = triple[2]*coef;
//System.out.println("and the new triple is: " + Arrays.toString(newtriple));
// System.out.println(Arrays.toString(triple) + " should STILL be (3,4,5).");
coef++;
}
}/*
if (found == false) {
newtriple = transform1(triple);
found = checkTriple(newtriple);
System.out.println("Here we are, in transform1");
}
if (found == false) {
newtriple = transform2(triple);
found = checkTriple(newtriple);
}
if (found == false) {
newtriple = transform3(triple);
found = checkTriple(newtriple);
}*/
return found;
}
}