-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnswer.java
More file actions
203 lines (182 loc) · 4.83 KB
/
Answer.java
File metadata and controls
203 lines (182 loc) · 4.83 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @Name Answer.java
* @author Zach Glassner
* This class is a reduction variable for solving Diophantine equations.
* The two subclasses provide reductions for finding the highest and lowest
* solutions with respect to x, y, then z.
*/
import edu.rit.io.InStream;
import edu.rit.io.OutStream;
import edu.rit.pj2.Tuple;
import edu.rit.pj2.Vbl;
import java.io.IOException;
public class Answer implements Vbl {
public long x;
public long y;
public long z;
/**
* Initializes an answer with x, y, and z set to 0.
*/
public Answer() {}
/**
* Initializes an answer with x, y, and z set to the given parameters.
* @param long x the x value of the answer.
* @param long y the y value of the answer.
* @param long z the z value of the answer.
*/
public Answer(long x, long y, long z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Returns the x value of the shared variable.
* @return long the x value.
*/
public long x() {
return this.x;
}
/**
* Returns the y value of the shared variable.
* @return long the y value.
*/
public long y() {
return this.y;
}
/**
* Returns the z value of the shared variable.
* @return long the z value.
*/
public long z() {
return this.z;
}
/**
* Reduces the given shared variable into this variable.
* @param Vbl shared variable
*/
public void reduce(Vbl vbl) {}
/**
* Set this answer to the given shared variable. This variable must
* be set to a deep copy of the given variable.
* @param vbl Shared variable.
*/
public void set(Vbl vbl) {
this.x = ((Answer) vbl).x();
this.y = ((Answer) vbl).y();
this.z = ((Answer) vbl).z();
}
/**
* Checks if a given set of x, y, and z satisfy our given Diophantine equation
* @param long x the x value to test
* @param long y the y value to test
* @param long z the z value to test
* @return boolean if the equation is a solution
*/
public static boolean isDioEqn(long x, long y, long z, int n, long c) {
return pow(x, n) + pow(y, n) == pow(z, n) + c;
}
/**
* Raises a base to an exponent. ie. base^exponent
* @param long base the number to raise to a power.
* @param int exponent the power to raise the base to.
* @return long base^exponent
*/
private static long pow(long base, int exponent){
if (exponent == 0){
return 1;
}
long calc = base;
for (int e = 1; e < exponent; e++) {
calc *= base;
}
return calc;
}
/**
* Prints out a Diophantine equation for a given Answer
* @param Answer a the answer to print out.
*/
public void printAnswer(int n, long c) {
System.out.println(this.x + "^" + n + " + " + this.y + "^" + n + " = " + this.z + "^" + n + " + " + c);
}
public Answer clone(){
try {
return (Answer) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException();
}
}
/**
* Class Answer.High provides reduction variable to find the highest answer
* with respect to x, then y.
*/
public static class High extends Answer {
/**
* Construct a new answer variable with x, y, and z set to 0.
*/
public High() {
super();
}
/**
* Initializes an answer with x, y, and z set to the given parameters.
* @param long x the x value of the answer.
* @param long y the y value of the answer.
* @param long z the z value of the answer.
*/
public High(long x, long y, long z) {
super(x, y, z);
}
/**
* Reduces the given shared variable into this variable. To get the highest
* solution with respect to x, then y.
* @param Vbl shared variable
*/
public void reduce(Vbl vbl) {
long x = ((Answer) vbl).x();
long y = ((Answer) vbl).y();
long z = ((Answer) vbl).z();
if (x > this.x || (x == this.x && y > this.y)
|| (x == this.x && y == this.y && z > this.z)) {
this.x = x;
this.y = y;
this.z = z;
}
}
}
/**
* Class Answer.Low provides reduction variable to find the lowest answer
* with respect to x, then y.
*/
public static class Low extends Answer {
/**
* Construct a new answer variable with x, y, and z set to 0.
*/
public Low() {
super();
}
/**
* Initializes an answer with x, y, and z set to the given parameters.
* @param long x the x value of the answer.
* @param long y the y value of the answer.
* @param long z the z value of the answer.
*/
public Low(long x, long y, long z) {
super(x, y, z);
}
/**
* Reduces the given shared variable into this variable. To get the lowest
* solution with respect to x, then y.
* @param Vbl shared variable
*/
public void reduce(Vbl vbl) {
long x = ((Answer) vbl).x();
long y = ((Answer) vbl).y();
long z = ((Answer) vbl).z();
if (x < this.x || (x == this.x && y < this.y)
|| (x == this.x && y == this.y && z < this.z)) {
this.x = x;
this.y = y;
this.z = z;
}
}
}
}