Advertisement
brilliant_moves

PythagoreanTriples.java

May 9th, 2015
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.22 KB | None | 0 0
  1. public class PythagoreanTriples {
  2.  
  3. /*
  4.     "In a right angled triangle, the square on the hypotenuse is equal to
  5.     the sum of the squares on the other two sides." - Pythagoras' Theorem.
  6. */
  7.  
  8.     /**
  9.     *   Program:    PythagoreanTriples.java
  10.     *   Purpose:    Calculate series of right-angled triangles
  11.     *   Creator     Chris Clarke
  12.     *   Created:    09.04.2014
  13.     *   Note:       This formula in this code finds some, but not all triples.
  14.     *           See also: https://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples
  15.     */
  16.  
  17.     private static long a=3L, b=4L, c=5L;
  18.  
  19.     public static void main(String[] args) {
  20.  
  21.         System.out.println("Triangles with the following length sides are right-angled:");
  22.  
  23.         while (a>0) {
  24.             a = calcSides(a);
  25.         } // end while
  26.  
  27.     } // end main()
  28.  
  29.     public static long calcSides(long a) {
  30.     // given a, calculate b and c and display them all
  31.  
  32.         // calculate new values
  33.         b = (a*a-1)/2;
  34.         c = b+1;
  35.  
  36.         if (c<0) {  // exceeded maximum long number, become negative
  37.             return -1;
  38.         } // end if
  39.  
  40.         // print sides of right-angled triangle
  41.         //System.out.println(a+" ^2 + "+b+" ^2 = "+c+" ^2");
  42.         System.out.println(a+", "+b+", "+c);
  43.         return c; // seed for next calcSides
  44.  
  45.     } // end calcSides()
  46.  
  47. } // end class PythagoreanTriples
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement