SpecialPythagoreanTriplet.java 657 B

123456789101112131415161718192021222324252627282930
  1. /* A Pythagorean triplet is a set of three natural numbers, a < b < c,
  2. for which, a2 + b2 = c2
  3. For example, 32 + 42 = 9 + 16 = 25 = 52.
  4. There exists exactly one Pythagorean triplet for which a + b + c = 1000.
  5. Find the product abc.
  6. */
  7. public class SpecialPythagoreanTriplet {
  8. private int triplet;
  9. public int findPythagoreanTriplet() {
  10. int product = 0;
  11. for (int a = 1; a < 1001; a++) {
  12. for (int b = 1; b < 1001; b++) {
  13. for (int c = 1; c < 1001; c++) {
  14. if (a + b + c == 1000 && (a*a + b*b) == c*c)
  15. product = a * b * c;
  16. else
  17. continue;
  18. }
  19. }
  20. }
  21. return product;
  22. }
  23. }