Advertisement
brilliant_moves

BigPythagoras.java

Jul 19th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.28 KB | None | 0 0
  1. import java.math.BigInteger;
  2.  
  3. public class BigPythagoras {
  4.  
  5.     /**
  6.     *   Program:    BigPythagoras.java
  7.     *   Purpose:    List of right-angled triangles whose sides are integers
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    20.07.2014
  10.     */
  11.  
  12.     public static void main(String[] args) {
  13.         BigInteger a = new BigInteger("3");
  14.         System.out.println("List of right-angled triangles whose sides are integers");
  15.         System.out.println("a^2 + b^2 = c^2");
  16.         System.out.println("===============");
  17.         for (int i=0; i<12; i++) {
  18.             a = calcSides(a);
  19.         } // end while
  20.     } // end main()
  21.  
  22.     public static BigInteger calcSides(BigInteger a) {
  23.     // given a, calculate b and c and display all 3 values
  24.  
  25.         /*
  26.             Pythagoras' Theorem states that:
  27.             "In a right-angled triangle, the square on the hypotenuse is equal
  28.             to the sum of the squares on the other two sides."
  29.  
  30.             a^2 + b^2 = c^2, where a, b and c are sides of a right-angled triangle,
  31.             and c is the hypotenuse.
  32.         */
  33.  
  34.         // b = ((a * a)-1)/2
  35.         BigInteger b = (a.multiply( a).subtract( BigInteger.ONE)).divide(new BigInteger("2"));
  36.         BigInteger c = b.add(BigInteger.ONE);
  37.  
  38.         // print sides of right-angled triangle
  39.         System.out.println(a+", "+b+", "+c);
  40.  
  41.         return c; // will become argument for next function call
  42.     } // end calcSides()
  43. } // end class BigPythagoras
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement