Sunday, 10 May 2015

Pythagorean triplet using O(n^2) complexity

Exported from Notepad++
//learnt triplet can be done in O(100^2) rather than O(100^3) public class Triplet { public static void main(String[] args) { int i,j,c; for(i=1;i<100;i++) for(j=i+1;j<Math.sqrt(10000-i*i);j++) { c=i*i+j*j; //each number in triplet should be less than 100 if(Math.sqrt(c)<100) { int cc=(int) Math.sqrt(c); // the below if statement is for checking whether the root of the C is integer or not if(cc==Math.sqrt(c)) System.out.println(" "+i+" "+j+" "+cc); } } } }


No comments:

Post a Comment