1 package com.sam.twisters.euler; 2 /* 3 * A Pythagorean triplet is a set of three natural numbers, 4 * a < b < c, for which, 5 * a^(2) + b^(2) = c^(2) 6 * For example, 3^(2) + 4^(2) = 9 + 16 = 25 = 5^(2). 7 8 * There exists exactly one Pythagorean triplet for which a + b + c = 1000. 9 * Find the product abc. 10 */ 11 public class Prog9 { 12 13 public static void main(String[] args) { 14 long startTime = System.nanoTime(); 15 16 for(int a=1;a<1000;a++){ 17 /* 18 * Optimization -->for(int b=1;b<1000;b++){ (5 char deleted 1000 added 'a') 19 * In a Pythagorean triplet a > b & c 20 */ 21 for(int b=1;b<a;b++){ 22 /* 23 * Optimization -->for(int c=1;c<1000;c++){(6 char deleted 1000 added '=b') 24 */ 25 for(int c=1;c<=b;c++){ 26 if(a*a == b*b+c*c && a+b+c == 1000){ 27 System.out.println(a*b*c); 28 /* 29 * Optimization -->a=1000; (7 char) 30 * At this point we have found our solution and we need to break out 31 * of the Loop. Breaking out of the outer for loop makes most sense 32 */ 33 a=1000; 34 } 35 } 36 } 37 } 38 39 long estimatedTime = System.nanoTime() - startTime; 40 System.out.println((float)estimatedTime/1000000000); 41 } 42 }