How can you print all the powers of two up to a given number in Java?

  • Context: Comp Sci 
  • Thread starter Thread starter Hiche
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around writing a Java program to print all positive integers that are powers of 2 and less than or equal to a given integer M. Participants explore different coding approaches, logic, and potential issues in their implementations.

Discussion Character

  • Homework-related, Technical explanation, Debate/contested

Main Points Raised

  • One participant presents an initial solution using the Math.pow method, questioning why the output is in double format instead of integer.
  • Another participant suggests an alternative approach using multiplication instead of the pow method to generate powers of 2.
  • A different implementation is shared, but the participant reports encountering negative values and zeros when inputting larger numbers.
  • One participant advises adding a break condition in the loop to prevent exceeding the limit of M.
  • Another participant shares a modified version of the code that works but notes it was unintentional and questions the logic behind the loop condition.
  • One participant expresses a preference for simpler solutions, arguing that they are generally better for readability and understanding.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to implement the solution, with no consensus on the best approach. There are differing opinions on the use of the Math.pow method versus multiplication, as well as on the clarity and effectiveness of various coding styles.

Contextual Notes

Some implementations may lead to unexpected results, such as negative numbers or zeros, depending on the logic used in the loop. The discussion does not resolve these issues or clarify the assumptions behind the proposed solutions.

Hiche
Messages
82
Reaction score
0

Homework Statement



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.

Homework Equations



for loops; if conditional.

The Attempt at a Solution



Code:
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?
 
Physics news on Phys.org
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.
 
Code:
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.
 
You should have some logic in your loop so that it exits after a > n.

Try this.
Code:
for (int i = 1; i <= n ; i++)
{
   a = a * 2;
   if (a <= n)
   {
      System.out.println(a);
   }
   else
   {
      break;
   }
}
 
Nevermind.

I think I got it.
 
Your version worked, but I did this:

Code:
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.
 
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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K