Search code examples
javanested-loopspythagorean

Second, if a is odd, b must be even and if a is even, then b must be odd, and c must be odd for either a / b combination


We have been doing labs in my computer science class and this was assigned as homework. I am supposed to make a program that find all Pythagorean triples from 1 to an entered number. I believe I can do that part but I am confused on another requirement. I have to check if that the integers I entered are even/odd, and also make sure the GCF of a, b, and c, are 1. Here are my instructions below along with the code I have written so far.

Instructions: Use nested loops to generate all of the Pythagorean triples from 1 up to a provided number. For three numbers to be a triple, they have to satisfy several requirements. First, the three numbers in the triple must satisfy the a2 + b2 == c2 . Second, if a is odd, b must be even and if a is even, then b must be odd, and c must be odd for either a / b combination. Lastly, the greatest common factor of a, b, and, c must be no greater than 1.

Also, at the end of my for loops to find the triplets, I tried to find if a was even or odd but I dont know how to check if a is odd then is b even as well as the GCF part. Please correct me if I made mistakes and give me advice on how to finish my coding assignment. Thanks in advance!

My Code:

package chapter4;

import java.util.*;
/**
 *
 * @author Anthony
 */
public class Triples {

    public static void main(String[]  args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("Choose a limit for the pythsgorean triplets");
        int limit = scan.nextInt();

        int a, b, c;

        for (a = 0; a < limit; a++) {

            for (b = 0; b < limit; b++) {

                for ( c = 0; c < limit; c++){
                    if( (Math.pow(a,2) + Math.pow(b, 2) == Math.pow(c, 2))) {
                        System.out.printf(" %d, %d, %d", a, b, c);
                    }   
                }
            }

            for (a = 0; a < limit; a++) {
               if (a % 2)
                   printf("%d is odd\n", a);
               else
                printf("%d is even\n", a);
            return 0;
        }
    }

Solution

  • You should consider putting the code to check whether a, b, and c are odd within the if statement which checks if a, b, and c are a Pythagorean triple. This way, the program will only check pythagorean triples to see if they are even or odd and what their gcd is. You can find if a number is even like this:

    if (a % 2 == 0) System.out.println("a is even");
    

    Another way is like this:

    if ((a & 1) == 0) System.out.println("a is even");
    

    This checks it based on the last bit, which determines whether the number is even or odd. It's a little faster but probably not the textbook answer your teacher is looking for (it's weird looking for early CS students).

    Then the else statement would catch all the odd answers. Also, don't forget your System.out. before printf in your if statements. You should also consider starting your for loops at 1, since the assignment has you test numbers from 1 to a limit. You can find the gcd either with the Euclidean Algorithm, which will require recursion (probably not what your teacher had in mind) or you can write a for loop to test numbers higher than 1, like this:

    int gcd = 1;
    for (int count = 2; count < a; count++){
       if (a % count == 0 && b % count == 0 && c % count == 0) gcd = count;
    }//close the for loop
    

    Side note: you can write either a, b, or c in the for loop continuation condition because the gcd has to be less than the smallest one. Testing values higher than the smallest (i.e. if b or c is the smallest for that scenario) will do nothing.

    Alternatively, you can write this as an if inside of an if inside of an if if you're not familiar with the && conjunction.

    Good luck