My issue is that even though it is printing out the values and everything correctly, I still need it to test higher values, obviously still less than my max value of my sample input, which is 110.
Here’s my code:
import static java.lang.System.*;
public class Triples
{
private int first;
private int second;
private int third;
private int number;
public Triples()
{
//this(0);
}
public Triples(int num)
{
number = num;
}
public void setNum(int num)
{
number = num;
}
private int greatestCommonFactor(int a, int b, int c)
{
int g;
int h;
if(a<b && a<c)
g = a;
else if(b< a && b<c)
g = b;
else
g = c;
for(int i = g; i > 0; i--)
{
if((a%i == 0) && (b%i == 0))
{
h = i;
for(int j = i; j>0; j--)
{
if((h%j==0) && (c%j == 0))
{
return j;
}
}
}
}
return -1;
}
public String check4Triples()
{
int max = number;
String amIdoneYet;
//int a;
//int b;
//int c;
for(int n = 1; n <= max; n++)
//{
for(int a = n; a <= max; a++)
{
first = a;
for(int b = a +1; b <= max; b++)
{
second =b;
for(int c = b + 1; c <= max; c++)
{
third = c;
if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
{
if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
{
if(this.greatestCommonFactor(a, b, c)== 1)
{
amIdoneYet = "";
amIdoneYet += a + " " + b + " "+ c;
return amIdoneYet;
}
}
}
}
}
}
return null;
}
public String toString()
{
String output=" ";
output += check4Triples() + " \n";
return output;
}
}
However, I believe the issue at hand lies in my runner class:
import static java.lang.System.*;
import java.util.Scanner;
public class Lab11j
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
String choice="";
do{
out.print("Enter the max number to use : ");
int big = keyboard.nextInt();
//instantiate a TriangleThree object
Triples triple = new Triples(big);
//call the toString method to print the triple
out.println( triple );
System.out.print("Do you want to enter more data? ");
choice=keyboard.next();
}while(choice.equals("Y")||choice.equals("y"));
}
}
Here’s what my output should look like:
- 3 4 5
- 5 12 13
- 7 24 25
- 8 15 17
- 9 40 41
- 11 60 61
- 12 35 37
- 13 84 85
- 16 63 65
- 20 21 29
- 20 99 101
- 28 45 53
- 33 56 65
- 36 77 85
- 39 80 89
- 48 55 73
- 60 91 109
- 65 72 97
UPDATE
After think it over some, I think the issue is that the for loops have no way of knowing whether or not to go again… I’m still thinking the issue relates back to my runner class somehow. But in my check4Triples method, I essentially only tell it to find the three lowest applicable numbers. I’m not sure how to tell it to check other values as well
The crux of the issue is that you return a string value immediately upon finding a GCF of one, whereas what you’re trying to do is loop through up to your max number and find all of the triples:
The second issue is that the loop(s) around the incorrect code has an extra loop,
1..n, which is already handled bya = 1..n:Now, you could solve this by:
Personally, I’d use a
List<String>for this and iterate over the output, but that’s a different issue.We end up with something closer to this:
Using
100as the max number, my output is this:Which I believe is closer to what you intend. I might use short-circuit logic, too: