import java.util.Scanner;

public class PTripleTableDriver{
    public static void main(String[] args){
        System.out.println("=== Pythag Triple Table Driver ===");
        Scanner kb = new Scanner(System.in);
        System.out.print("\nHow many triples? ");
        int n = kb.nextInt();
        if(n < 1) n = 1;
        
        String tripTable[][] = new String[n+2][7];
        
        tripTable[0][0] = "n";
        tripTable[0][1] = "x";
        tripTable[0][2] = "y";
        tripTable[0][3] = "|";
        tripTable[0][4] = "a";
        tripTable[0][5] = "b";
        tripTable[0][6] = "c";
        tripTable[1][0] = "-";
        tripTable[1][1] = "-";
        tripTable[1][2] = "-";
        tripTable[1][3] = "-";
        tripTable[1][4] = "-";
        tripTable[1][5] = "-";
        tripTable[1][6] = "-";
        
        int x;
        int y;
        int a, b, c;
        int row = 2;
        int col;
        int count = 0;
        int cMax = 1; //used to get the max table column width
        
        //dwr! if the upperbound of x is not big enough
        //not enough entries will be generated
        for(x = 2; x < n+2; x++){
            for(y = x-1; y > 0; y--){
                count++;
                a = x*x - y*y;
                b = 2*x*y;
                c = x*x + y*y;
                //System.out.println("a, b, c = " + a + ", " + b + ", " + c);
                tripTable[row][0] = Integer.toString(count);
                tripTable[row][1] = Integer.toString(x);
                tripTable[row][2] = Integer.toString(y);
                tripTable[row][3] = "|";
                tripTable[row][4] = Integer.toString(a);
                tripTable[row][5] = Integer.toString(b);
                tripTable[row][6] = Integer.toString(c);
                if(c > cMax) cMax = c;
                row++;
                if(count == n) break;
            }//end y loop
            if(count == n) break; //dwr! must break out of 2nd loop too!
        }//end x loop
        
        //System.out.println("...out of loop...");
        //System.out.println("cMax = " + cMax);
        double powTen = Math.log10(cMax);
        //System.out.println("powTen = " + powTen);
        int colWidth = (int)(Math.ceil(powTen) + 2);
        System.out.println();
        //System.out.println("colWidth = " + colWidth);
        print2DArray(tripTable, colWidth);
        
        System.out.println("\nThanks for using our program!");
        
        
    }//end main
    
    public static void print2DArray(String arr[][], int maxColWidth){
        int numRows = arr.length;
        int numCols = arr[0].length;
        for(int r = 0; r < numRows; r++){
            for(int c = 0; c < numCols; c++){
                String valStr =  arr[r][c];
                String fString = String.format("%" + maxColWidth + "s", valStr);
                System.out.print(fString);
            }//end c loop
            System.out.println(); //dwr! don't forget to move to new line
        }//end r loop
    }//end print2DArray
    
}//end class