Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Add This Thread To: Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb
 « Previous Thread | Next Thread » Thread Tools Search this Thread Rate Thread Display Modes
 Dev Shed Forums Sponsor:
#1
September 26th, 2010, 07:06 PM
 dannyping
Registered User

Join Date: Sep 2010
Posts: 8
Time spent in forums: 2 h 35 m 10 sec
Reputation Power: 0
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.

#2
September 26th, 2010, 08:22 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 11 m 2 sec
Reputation Power: 347
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 ...

#3
September 26th, 2010, 08:42 PM
 dannyping
Registered User

Join Date: Sep 2010
Posts: 8
Time spent in forums: 2 h 35 m 10 sec
Reputation Power: 0
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;

#4
September 26th, 2010, 08:44 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 11 m 2 sec
Reputation Power: 347
Can you explain your algorithm for solving the problem in English?

#5
September 26th, 2010, 10:28 PM
 dannyping
Registered User

Join Date: Sep 2010
Posts: 8
Time spent in forums: 2 h 35 m 10 sec
Reputation Power: 0
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.

#6
September 26th, 2010, 11:28 PM
 Wetmelon
Contributing User

Join Date: Oct 2009
Location: London, ON
Posts: 322
Time spent in forums: 3 Days 9 h 49 m 24 sec
Reputation Power: 214
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
Quote:
 Originally Posted by DaWei_M That covers a multitude of your sins, right there.

#7
September 26th, 2010, 11:34 PM
 tvc3mye
Daniel Schildsky

Join Date: Mar 2004
Location: KL, Malaysia.
Posts: 1,540
Time spent in forums: 2 Weeks 4 Days 4 h 14 m
Reputation Power: 1620
Perhaps using logarithm?

Maybe using natural logarithm is better in this case.
1. set base = log (2)
2. set value = log(number)
3. set nearest = floor (value / base)
4. set answer = 2^nearest
5. print answer.
__________________
When the programming world turns decent, the real world will turn upside down.

#8
September 27th, 2010, 08:43 AM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 11 m 2 sec
Reputation Power: 347
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

#9
September 27th, 2010, 10:02 AM
 codeJ
Santosh Vaza

Join Date: Jun 2010
Location: JHome
Posts: 356
Time spent in forums: 3 Days 9 h 21 m 32 sec
Warnings Level: 3
Reputation Power: 12
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
Quote:
 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
spyder0101 disagrees: Teach a man to fish...
f_nietzsche agrees: No love for codeJ, regardless?
__________________
no one can become perfect by merely ceasing the act

Last edited by codeJ : September 27th, 2010 at 10:05 AM.

#10
September 27th, 2010, 11:01 AM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 11 m 2 sec
Reputation Power: 347
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.

#11
September 27th, 2010, 03:13 PM
 tvc3mye
Daniel Schildsky

Join Date: Mar 2004
Location: KL, Malaysia.
Posts: 1,540
Time spent in forums: 2 Weeks 4 Days 4 h 14 m
Reputation Power: 1620
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 step-by-step 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?

#12
October 1st, 2010, 07:46 AM
 codeJ
Santosh Vaza

Join Date: Jun 2010
Location: JHome
Posts: 356
Time spent in forums: 3 Days 9 h 21 m 32 sec
Warnings Level: 3
Reputation Power: 12
Quote:
 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 ....

#13
October 1st, 2010, 09:08 AM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 11 m 2 sec
Reputation Power: 347
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.

#14
October 2nd, 2010, 12:39 AM
 NovaX
Contributing User

Join Date: Jul 2005
Location: Bay Area, California
Posts: 841
Time spent in forums: 3 Weeks 5 Days 12 h 59 m 16 sec
Reputation Power: 1681
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 most-significant 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.
__________________
Core design principles when developing software systems.
See my open-source project as an example of professional code.
---
The opinions expressed do not represent those of my employer.

Last edited by NovaX : October 2nd, 2010 at 03:55 PM.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Smallest Power of 2

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap