Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:14:21+00:00 2026-06-15T05:14:21+00:00

My issue is that even though it is printing out the values and everything

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:14:21+00:00Added an answer on June 15, 2026 at 5:14 am

    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:

    if (this.greatestCommonFactor(a, b, c) == 1) {
        return "" + a + " " + b + " "+ c;
    }
    

    The second issue is that the loop(s) around the incorrect code has an extra loop, 1..n, which is already handled by a = 1..n:

    for (int n = 1; n <= max; n++) {
        for (int a = n; a <= max; a++) {
            ...
    

    Now, you could solve this by:

    1. Eliminating the extra loop, and
    2. Adding the triple to a single string following by a newline.

    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:

    public String check4Triples() {
        int max = number;
        String ret = "";
    
        for (int a = 1; a <= max; a++) {
            for (int b = a + 1; b <= max; b++) {
                for (int c = b + 1; c <= max; c++) {
                    if (isPythagoreanTriple(a, b, c)) {
                        if (mod2(a, b) || mod2(b, a)) {
                            if (greatestCommonFactor(a, b, c) == 1) {
                                ret += "" + a + " " + b + " " + c + "\n";
                            }
                        }
                    }
                }
            }
        }
    
        return ret;
    }
    

    Using 100 as the max number, my output is this:

    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
    28 45 53
    33 56 65
    36 77 85
    39 80 89
    48 55 73
    65 72 97
    

    Which I believe is closer to what you intend. I might use short-circuit logic, too:

    if (isPythagoreanTriple(a, b, c)
            && (mod2(a, b) || mod2(b, a))
            && (greatestCommonFactor(a, b, c) == 1)) {
        ret += "" + a + " " + b + " " + c + "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange issue that has arisen recently: Whenever I enter text, even
I am having an issue that I can't seem to figure out. Hopefully somebody
So I have an issue that I'm not 100% sure on how to solve.
I have an issue that (I think) might have to do with scope, but
I have an issue that is driving me nuts and hoping someone can help.
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have an issue that seems like very flaky behavour, is this a problem
I have an issue that looks like a race condition with a webview callback
There is an issue that I cannot solve, I've been looking a lot in
I've had an issue that I thought was tinyMCE, then my db, now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.