Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PythagoreanTriples2 {
- /*
- "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: PythagoreanTriples2.java
- * Purpose: Calculate series of right-angled triangles
- * Creator Chris Clarke
- * Created: 09.05.2015
- * Note: Calculates all triples from a=3 to a=499.
- * See also: https://en.wikipedia.org/wiki/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<=500L) {
- calcSides(a);
- a+=2;
- } // end while
- } // end main()
- public static void calcSides(long a) {
- // given a, calculate b and c and display them all
- /*
- a is odd (Pythagoras, c. 540 BC):
- side a : side b = (a^2 - 1)/2: side c = (a^2 +1)/2
- */
- // calculate new values
- b = (a*a-1)/2;
- c = (a*a+1)/2;
- if (c<0) { // exceeded maximum long number, become negative
- System.exit(0);
- } // end if
- if (!(a*a + b*b == c*c)) {
- return;
- } // if
- // print sides of right-angled triangle
- System.out.println(a+", "+b+", "+c);
- } // end calcSides()
- } // end class PythagoreanTriples2
Advertisement
Add Comment
Please, Sign In to add comment