Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Java powers of two question.

  1. Oct 20, 2011 #1
    1. The problem statement, all variables and given/known data

    Write a program that takes a command line argument integer M and prints all the positive integers that are powers of 2 and less than or equal to M.

    2. Relevant equations

    for loops; if conditional.

    3. The attempt at a solution

    Code (Text):
    public class PowersOfTwo
    {
        public static void main(String[] args)
        {
            int m = Integer.parseInt(args[0]);

            for (int i = 0; i <= m ; i++)
            {
                if (Math.pow(2, i) <= m)
                    System.out.println(Math.pow(2, i));
            }
        }
    }
    Is this true and answers the problem? Why are the values printed in type double rather than integer? Like so, when I input 4 in the command-line:

    1.0
    2.0
    4.0


    And is there another way to do that using boolean? How do you check if an integer is a power of two? Do you use the modulus or what?
     
  2. jcsd
  3. Oct 20, 2011 #2

    Mark44

    Staff: Mentor

    Because the pow method returns a double, not an int.

    You can do this without using the pow method, by instead using nothing more complicated that multiplication.
    2 = 21 = 2
    4 = 22 = 2*2
    8 = 23 = 2*2*2
    and so on.
     
  4. Oct 23, 2011 #3
    Code (Text):
    public class Power
    {
        public static void main(String[] args)
        {
            int n = Integer.parseInt(args[0]);
            int a = 2;

            for (int i = 1; i <= n ; i++)
            {
                a = a * 2;
                if (a <= n)
                {
                    System.out.println(a);
                }
            }
        }
    }
    Is this fine? Whenever I input a number like 99, it works fine for the first couple of values then a negative number appears and a series of 0s appear out of nowhere.
     
  5. Oct 23, 2011 #4

    Mark44

    Staff: Mentor

    You should have some logic in your loop so that it exits after a > n.

    Try this.
    Code (Text):

    for (int i = 1; i <= n ; i++)
    {
       a = a * 2;
       if (a <= n)
       {
          System.out.println(a);
       }
       else
       {
          break;
       }
    }
     
  6. Oct 23, 2011 #5
    Nevermind.

    I think I got it.
     
  7. Oct 23, 2011 #6
    Your version worked, but I did this:

    Code (Text):
    public class Power
    {
        public static void main(String[] args)
        {
            int n = Integer.parseInt(args[0]);
            int a = 1;

            for (int i = 1; i <= n * i / a; i++)
            {
                a = a * 2;
                if (a <= n)
                {
                    System.out.println(a);
                }
            }
        }
    }
    ..and it worked. I unintentionally did that, though; the i <= n * i /a.
     
  8. Oct 23, 2011 #7

    Mark44

    Staff: Mentor

    I like my solution better because it is simpler. A simpler solution is usually better than a clever solution, because it will be easier for some other person reading your code to understand.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook