Thursday 29 December 2016

Maximum height when coins are arranged in a triangle


public class MaxHghtToArrangeTri {
    
     /**
      *         *           ß 1 coin
      *   *         *       ß 2 coin
      * *      *          * ß 3 coin
      *
      * Coins need for Height H = 1 + 2 + 3 + ........ + H-1
      *                                 = H(H-1)/2
      * @param coins
      * @return
      */
     private static int getMaxHeight(int coins) {
          
           int d = (int)Math.pow((8*coins + 1),.5);
          
           int height = (d + 1)/2;
          
           return height-1;
     }
    
     /**
      * @param args
      */
     public static void main(String[] args) {
           int coins = 7;
           int maxHeight = getMaxHeight(coins);
          
           System.out.println(maxHeight);
     }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...