### Triangle problem

How many triangles with sides of integer length have a sum of sides being a particular number?  This program calculates this for sum of sides up to the number specified.  It displays the result in raw form (listing all the possible triangles) or as tabulated totals of the number of triangles adding up to any particular sum.  The results are displayed in the Java Console.  There is a pattern to the results, but the pattern is not simple.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Triangle extends Applet implements ActionListener {

TextField textField;
Button rawButton, totalsButton;
static final boolean RAW = true;

public void init()
{
Label label = new Label ("Specify highest sum of side lengths");
textField = new TextField(6);
rawButton = new Button("Calculate raw");
totalsButton = new Button("Calculate totals");
}

final void calculate(boolean raw)
{
int biggestTotal;
try
{
biggestTotal = Integer.parseInt(textField.getText().trim());
}
catch (Exception e)
{
biggestTotal = 10;
}
int biggestSide = ((biggestTotal + 1)/2) - 1;
int[] totalForNumber = new int[biggestTotal+1];
for (int a=1; a<= biggestSide; a++) for (int b=1; b<=a; b++) for (int c=1; c<=b; c++) if ((a<b+c) && (a+b+c <= biggestTotal))
{
if (raw) System.out.println(a + "\t" + b + "\t" + c + "\t" + (a+b+c));
else totalForNumber[a+b+c]++;
}
if (!raw) for (int i=0; i<biggestTotal+1; i++) System.out.println(i + "\t" + totalForNumber[i]);
}

public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == rawButton) calculate(RAW);
else if (ae.getSource() == totalsButton) calculate(!RAW);
}

} // END OF Class Triangle