#include <stdio.h>
#include <math.h>
#define PI 3.14159265359

/*

This program enumerates all the pythagorean triples where the
hypothenuse is shorter than the integer inputted by the user.
It also states which of the triangle in
the solution set is the thinnest.

*/

int main(void)
{
    /*Identifying Variable*/

    int sideC, counter = 0, posN;
    double sideA, sideB, x, yAngle, curAngle, i, j, k;
    curAngle = 360;

    printf("Pythagorean Triples:\n");

    /*User inputs integer*/

    printf("Please input a positive integer:");
    scanf("%d", &posN);

    /*looping to fin all possible triples*/

    for ( sideA = 1; sideA < posN; ++sideA )
    {
    for ( sideB = sideA + 1; sideB < posN; ++sideB )
    {
    for ( sideC = sideB + 1; sideC < posN; ++sideC )
    {
    if ( sideA * sideA + sideB * sideB == sideC * sideC )
    {
    ++counter;
    }
    }
    }
    }

    /*Prints total options*/

    printf("\nThere is/are %3d pythagorean triples\n\n", counter);

    counter = 0;

    /*Loops to find the thinnest triangle in solution set*/

    for ( sideA = 1; sideA < posN; ++sideA )
    {
    for ( sideB = sideA + 1; sideB < posN; ++sideB )
    {
    for ( sideC = sideB + 1; sideC < posN; ++sideC )
    {
    if ( sideA * sideA + sideB * sideB == sideC * sideC )
    {

    x = atan(sideA/sideB);
    yAngle = x*(180/PI);

    if(yAngle < curAngle)
    {

    curAngle = yAngle;
    i = sideA;
    j = sideB;
    k = sideC;
    }

    printf("%3d : (%1.0f,%1.0f,%d)\n", ++counter, sideA, sideB, sideC);

    }
    }
    }
    }

    printf("\n\nThere is/are %3d pythagorean triples\n", counter);

    /*Prints thinnest triangle test results*/

    printf("The thinnest triangle has smallest angle %3f, and is formed by (%1.0f,%1.0f,%1.0f) \n\n\n\n", curAngle, i, j, k);
    return 0;
}
