Advertisement
Guest User

Algo Code

a guest
Aug 3rd, 2016
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. public class AbcdSquaredEqualThousand {
  2.  
  3.     public static void main(String[] args) {
  4.         long tempLong = System.currentTimeMillis();
  5.         doUnoptimised();
  6.         System.out.println("MS Taken:" + (System.currentTimeMillis() - tempLong));
  7.         tempLong = System.currentTimeMillis();
  8.         doFirstOptimised();
  9.         System.out.println("MS Taken:" + (System.currentTimeMillis() - tempLong));
  10.  
  11.     }
  12.    
  13.     public static void doUnoptimised() {
  14.         for(int a = 1; a < 100; a++)
  15.         {
  16.             for(int b = 1; b < 100; b++)
  17.             {
  18.                 for(int c = 1; c < 100; c++)
  19.                 {
  20.                     for(int d = 1; d < 100; d++) {
  21.                         if(Math.pow(a, 3) + Math.pow(b, 3) == Math.pow(c, 3) + Math.pow(d, 3))
  22.                         {
  23.                             System.out.println("A:" + a + " B:" + b + " C:" + c + " D:" + d);
  24.                         }
  25.                     }
  26.                 }
  27.             }
  28.         }
  29.     }
  30.    
  31.     public static void doFirstOptimised() {
  32.         for(int a = 1; a < 100; a++)
  33.         {
  34.             for(int b = 1; b < 100; b++)
  35.             {
  36.                 for(int c = 1; c < 100; c++)
  37.                 {
  38.                     for(int d = 1; d < 100; d++) {
  39.                         if(Math.pow(a, 3) + Math.pow(b, 3) == Math.pow(c, 3) + Math.pow(d, 3))
  40.                         {
  41.                             System.out.println("A:" + a + " B:" + b + " C:" + c + " D:" + d);
  42.                             break;
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement