Solve for 2^x

Discussion in 'AutoCAD' started by Andy Elmore, May 23, 2004.

  1. Andy Elmore

    Andy Elmore Guest

    Okay, I need to solve this programming question.
    I have a the value 148 which equals 2^2 + 2^4 + 2^7 how can I come up with a formula that will solve for 148

    Andy
     
    Andy Elmore, May 23, 2004
    #1
  2. Please clarify: are you attempting to convert an arbitrary number to binary?

    At first, I thought you were going for prime factors but that's not it at
    all.
     
    Frank Oquendo, May 23, 2004
    #2
  3. Andy Elmore

    wivory Guest

    Alright, I *think* I know what you're after. You question asks how to solve for 148, but your subject asks how to solve for x, so I'm going to assume you want an algorithm that for any number x will produce a solution that is the sum of powers of 2.

    This algorithm assumes that there *is* a solution that can be derived using whole numbers.

    I remember from my high-school math (amazingly!) that if a^b=c then log(c)/log(a)=b. If we look at your example of 148, we can find that log(148)/log(2)=7.2094. We can see that's "seven and a bit" so we'll take the first part of the solution as 2^7 which is 128. We subtract that from the original 148 and we're left with 20.

    log(20)/log(2)=4.3219 which is "4 and a bit" so the next part of the solution is 2^4 which is 16. We subtract that from 20 and we're left with 4.

    log(4)/log(2)=2 exactly so the final part of the solution is 2^2.

    I hope this makes sense.

    Regards

    Wayne Ivory
    IT Analyst Programmer
    Wespine Industries Pty Ltd
     
    wivory, May 24, 2004
    #3
  4. Andy Elmore

    Andy Elmore Guest

    That was exactly the answer. I’m not sure why I did not see it (maybe I was asleep in class that day :))

    Thanks,
    Andy
     
    Andy Elmore, May 24, 2004
    #4
  5. Andy Elmore

    aks Guest

    Good one Wayne. :) I would have used DEC2BIN(148) and picked off the
    bits. Since 2^0 = 1, I think there always is a solution.

    AKS
     
    aks, May 25, 2004
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.