September 26th, 2010, 07:06 PM

Smallest Power of 2
I have to create a program that when the user enters a number greater than or equal to zero. The program will return the smallest power of two that is greater than or equal to that number.
I have to use a while statement. How do I go about doing this?
I already have an if statement displaying an error message if the user inputs a number that is less than 0.
September 26th, 2010, 08:22 PM

That seems like an easy enough problem.
What are your ideas on how to solve it?
What are your problems?
Can you generate powers of 2? 1 2 4 8 ...
September 26th, 2010, 08:42 PM

Its just when a user enters a number, I just need to return the smallest power of two for that number. I was given this but dont quite understand how to use it. Its broken code as I know im missing things.
int x =0;
while(Math.pow(x,2) < num){ x++
for (in x=0; x<num;x++){math.pow(x,2) >=num){ System.out.println(x);break;
September 26th, 2010, 08:44 PM

Can you explain your algorithm for solving the problem in English?
September 26th, 2010, 10:28 PM

Our java lab teacher gave it to us. I'm not sure but looking at it I can only guess that it means that if the x int is less than the number given then the ++ means add two to it? But i dont see how that would make sense.
September 26th, 2010, 11:28 PM

No, not at all. x++ means add one to x. It's equivalent to x = x+1;
If you were going to solve this problem in English, how would you do it? How about "I would take each ascending power of two, and compare it to the inputted number to see if it is greater than or equal to it. And once that's true, I would be done."
Now you have a logical direction for your program. It's a fairly simple matter of converting this to code. If going directly there is too advanced at this moment, try doing pseudo code first, such as "loop for each power of two, while it's less than num" something like that. You get the idea  it's a series of steps to break a large problem down into a smaller one.
Note: It'll probably be better if you ignore the code your TA gave you for now, since you probably haven't been to class... _
<Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
<Tetrad> I don't know whether to be proud or scared to death
Originally Posted by DaWei_M
That covers a multitude of your sins, right there.
September 26th, 2010, 11:34 PM

Perhaps using logarithm?
Maybe using natural logarithm is better in this case.
 set base = log (2)
 set value = log(number)
 set nearest = floor (value / base)
 set answer = 2^nearest
 print answer.
When the programming world turns decent, the real world will turn upside down.
September 27th, 2010, 08:43 AM

Forget about writing the code for now. You need to design the program BEFORE you write the code.
Can you explain your algorithm for solving the problem in English?
Step one,
step two,
step three,
etc until you've got the answer
September 27th, 2010, 10:02 AM

what i suggest is the following Algo
> a loop that iterates from i=1 to <entered_number> step by 1
> Look for Math Class pow() , so in that loop you will check for condition whether
pow(2,i <current_value in loop>) > <entered_number> or == <entered_number>
> when condition gets true then thats it ... you get
Originally Posted by dannyping
smallest power of two that is greater than or equal to that number.
EDIT:
Also when condition gets true don't forget to break loop, iterating further will be useless as you got the answer...
Comments on this post
Last edited by codeJ; September 27th, 2010 at 10:05 AM.
no one can become perfect by merely ceasing the act
September 27th, 2010, 11:01 AM

codeJ
You're spoiling it for the OP. How is he going to learn if someone gives him the answers? At least let the OP try to find the algorithm before you give it.
You don't learn without trying. Reading someone else's work is not the same thing.
September 27th, 2010, 03:13 PM

Apology
Sorry, was reading the post in hurry, suggested a different algorithm instead.
Basically you need to think of the loop as a sentinel, to ensure that the loop stops when the power of 2 is greater than the current number. You will have to keep on calculating the power of 2 from scratch until you found an answer.
All the nuts and bolts are in the codes provided by your instructor, but it is not quite in the right sequence and there are some syntax errors here and there. Try sorting out a systematic stepbystep way of how you would solve this problem, involving a loop.
Are you stucked at the Java syntax or are you having problem getting that algorithm provided by your instructor?
When the programming world turns decent, the real world will turn upside down.
October 1st, 2010, 07:46 AM

Originally Posted by NormR
You're spoiling it for the OP
was just helping ...
Still i gave a short algo and made the OP to code by himself ....
no one can become perfect by merely ceasing the act
October 1st, 2010, 09:08 AM

Yeah, its hard to know how much to feed an OP to get the light to come on. Some take more, some less.
Wonder if the OP every figured it out.
October 2nd, 2010, 12:39 AM

An elegant and efficient approach to this problem is to use binary. It is sometimes better to understand how the computer works than just mathematics, because doing so can lead to impressive performance gains. I'll describe how I'd calculate the positive power of two (no two's compliment complexity)
A positive power of two has trailing zeros after the mostsignificant bit (MSB). For example,
2^0 = 0001
2^1 = 0010
2^2 = 0100
2^3 = 1000
First we determine if the input is a power of two by calculating the population count. This should be one. Integer#bitCount() is our friend here.
If its not, then we need to determine the MSB of our input, which would give us the next smallest power of two. Here Integer#highestOneBit() is handy.
With that we can perform a left shift on the value 1 to give us the next highest power. Integer#rotateLeft() is handy.
The code should be about as simple to write as other's suggested, but much faster.
Last edited by NovaX; October 2nd, 2010 at 03:55 PM.
Core
design principles when developing software systems.
See my
opensource project as an example of professional code.

The opinions expressed do not represent those of my employer.