Reputation: 1469
I am trying to find a given set of numbers for the equation
x^3 + y^3 = z^3 +1
where
x < y < z
the code below is what I have started working on. The problem I am currently having is the random numbers i generate only generate on the first run through the program and i cannot figure out why any help or clues as to how to improve my code would be greatly appreciated.
import java.util.Random;
public class etude14 {
static int x = 1;
static int y = 2;
static int z = 3;
static int matchCount = 0;
public static void main(String[] args) {
while(matchCount < 23){
equatition(x, y, z);
}
}
public static void equatition(int x, int y, int z) {
double leftResult = Math.pow(x, 3) + Math.pow(y, 3);
double rightResult = Math.pow(z, 3) + 1;
if (leftResult == rightResult) {
System.out.println("Match " + x + " " + y + " " + z);
matchCount++;
changeX();
} else {
System.out.println("No Match " + x + " " + y + " " + z);
changeX();
}
}
private static void changeX() {
Random generator = new Random();
int x2 = generator.nextInt(10000) + 1;
int y2 = generator.nextInt(10000) + 1;
int z2 = generator.nextInt(10000) + 1;
if(x < y && y < z){
System.out.println("WE HAVE NEW X,Y,Z");
x = x2;
y = y2;
z = z2;
return;
}
System.out.println("CHANGING X");
}
}
code after first answer
import java.util.Random;
public class etude14 {
static int x = 1;
static int y = 2;
static int z = 3;
static int matchCount = 0;
public static void main(String[] args) {
while (matchCount < 23) {
equatition(x, y, z);
}
}
public static void equatition(int x, int y, int z) {
double leftResult = Math.pow(x, 3) + Math.pow(y, 3);
double rightResult = Math.pow(z, 3) + 1;
if (leftResult == rightResult) {
System.out.println("Match " + x + " " + y + " " + z);
matchCount++;
changeX();
} else {
System.out.println("No Match " + x + " " + y + " " + z);
changeX();
}
}
private static void changeX() {
Random generator = new Random();
int x2 = 1;
int y2 = 1;
int z2 = 1;
if (x < y && y < z) {
System.out.println("WE HAVE NEW X,Y,Z");
x = x2;
y = y2;
z = z2;
return;
} else {
x2 = generator.nextInt(10000) + 1;
y2 = generator.nextInt(10000) + 1;
z2 = generator.nextInt(10000) + 1;
System.out.println("CHANGING X");
}
}
}
output
No Match 1 2 3
WE HAVE NEW X,Y,Z Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X Match 1 1 1 CHANGING X
Upvotes: 1
Views: 1075
Reputation: 19788
Your random genetator 'stops' generating when z becomes a small number.
In this case the condition if(x2 < y2 && y2 < z2)
will rarely be verified so your number won't change.
You'll need something like
private static void changeX() {
Random generator = new Random();
int x2 = generator.nextInt(10000) + 1;
int y2 = generator.nextInt(10000) + 1;
int z2 = generator.nextInt(10000) + 1;
x = Math.min(Math.min(x2, y2), z2); // The Max of the 3 numbers
z = Math.max(Math.max(x2, y2), z2); // The Min of the 3 numbers
if (x != x2 && z != x2) { // The remaining middle number
y = x2;
} else if (x != y2 && z != y2) {
y = y2;
} else {
y = z2;
}
}
or maybe
private static void changeX() {
Random generator = new Random();
List<Integer> listInt = new ArrayList<Integer>();
int x2 = generator.nextInt(10000) + 1;
listInt.add(x2);
int y2 = generator.nextInt(10000) + 1;
listInt.add(y2);
int z2 = generator.nextInt(10000) + 1;
listInt.add(z2);
Collections.sort(listInt);
x = listInt.get(0);
y = listInt.get(1);
z = listInt.get(2);
}
Upvotes: 1
Reputation: 14810
One problem is here
if(x < y && y < z){
x = x2;
y = y2;
z = z2;
return;
}
You have initialized your static ints x, y, and z to 1, 2, and 3 respectively, so your test of x < y is true and y < z is true, and you therefore execute the block assigning x2 to x etc.
But there is no guarantee that x2 < y2 and y2 < z2 for your randomly selected numbers. The new values are set the first time you hit that if-statement but are unlikely to be set anytime after that, except in the unlikely event that your random x2, y2, and z2 happen to be in ascending order.
What you need is a loop in changeX()
to continue generating random numbers until x2 < y2 && y2 < z2
and then assign the new x, y, and z values. Something like
do {
... // set new random values for x2, y2, and z2
} while (! (x2 < y2 && y2 < z2) );
Be warned, however, that this loop can possibly run for a very long time until you happen to get 3 values in ascending order.
Random values aren't a good approach in general to seeking a solution to an equation -- you could try the same 3 values repeatedly.
Given x2 and y2 such that x2 < y2 there is a lower limit on the value of z2 that could possibly solve the equation. One approach could be
x2 = random number from 1 to 10,000
y2 = random number from x2+1 to 10,000
z2 = method_to_guess_a_lower_limit_for_z2(x2, y2);
Even this approach is hugely naive -- as @HotLicks mentions in a comment, some variation on Newton's Method of approximation is a better place to start.
Upvotes: 1
Reputation: 1379
if(x < y && y < z){
System.out.println("WE HAVE NEW X,Y,Z");
x = x2;
y = y2;
z = z2;
return;
}
Well try this:
if(x2 < y2 && y2 < z2){
System.out.println("WE HAVE NEW X,Y,Z");
x = x2;
y = y2;
z = z2;
return;
}
else{
//repeat the procedure for generating random numbers.
// You have one-sixth possibility but that is not quite much.
}
Upvotes: 1