Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PythagoreanTriples {
- /*
- "In a right angled triangle, the square on the hypotenuse is equal to
- the sum of the squares on the other two sides." - Pythagoras' Theorem.
- */
- /**
- * Program: PythagoreanTriples.java
- * Purpose: Calculate series of right-angled triangles
- * Creator Chris Clarke
- * Created: 09.04.2014
- * Note: This formula in this code finds some, but not all triples.
- * See also: https://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples
- */
- private static long a=3L, b=4L, c=5L;
- public static void main(String[] args) {
- System.out.println("Triangles with the following length sides are right-angled:");
- while (a>0) {
- a = calcSides(a);
- } // end while
- } // end main()
- public static long calcSides(long a) {
- // given a, calculate b and c and display them all
- // calculate new values
- b = (a*a-1)/2;
- c = b+1;
- if (c<0) { // exceeded maximum long number, become negative
- return -1;
- } // end if
- // print sides of right-angled triangle
- //System.out.println(a+" ^2 + "+b+" ^2 = "+c+" ^2");
- System.out.println(a+", "+b+", "+c);
- return c; // seed for next calcSides
- } // end calcSides()
- } // end class PythagoreanTriples
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement