Advertisement
NYCJacob

largest power

Sep 9th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.38 KB | None | 0 0
  1.  
  2. /**
  3.  * Complete the method to find the largest power of 2 less than the given number
  4.  * Use a loop
  5.  */
  6. public class MathUtil
  7. {
  8.    public int largestPowerOf2(int n)
  9.    {
  10.        double pow = 0;
  11.        while ( Math.pow(2, pow) < n)
  12.        {
  13.           // pow = Math.pow(2.0, pow);
  14.            //pow = pow * 2;
  15.            pow++;
  16.        }
  17.        
  18.        return (int)pow;
  19.    }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement