Solve for 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. Probably could be coded tighter and named better but ...

    Code:
    (defun GetBase2BitsSet ( int )
    (vl-remove-if 'null
    (mapcar
    '(lambda (bit / x)
    (if (eq (setq x (expt 2 bit)) (logand int x)) bit)
    )
    '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
    )
    )
    )
    
    (GetBase2BitsSet 148) returns (2 4 7)

    Cross posting? Tsk, tsk.
     
    michael puckett, May 23, 2004
    #2
  3. (setq x (+ (expt 2 2) (expt 2 4) (expt 2 7)))
     
    Thomas 'bacco|007' Baxter, May 24, 2004
    #3
  4. Andy Elmore

    David Bethel Guest

    I'd go about it like this:

    (defun solve_X (n / sum i elist)
    (setq i 2)
    (while (> n sum)
    (setq i (1+ i)
    sum (expt 2 i)))
    (while (> (setq i (1- i)) 1)
    (if (= (logand n (expt 2 i)) (expt 2 i))
    (setq elist (cons i elist))))
    elist)

    -David
     
    David Bethel, May 24, 2004
    #4
  5. Andy Elmore

    David Kozina Guest

    How 'bout:

    (setq x 148)

    :)


    with a formula that will solve for 148
     
    David Kozina, May 24, 2004
    #5
  6. Andy Elmore

    Doug Broad Guest

    Fellow Lispers,

    Before spending any more time puzzling on this thread, see
    Andy's response to the same thread on the VBA newsgroup.

    Thought you'd like to know.

    Regards,
    Doug
     
    Doug Broad, May 24, 2004
    #6
  7. Andy Elmore

    Andy Elmore Guest

    Sorry, I forgot to post that I got the answer I was looking for. Log(x,2)

    Thanks,
    Andy
     
    Andy Elmore, May 27, 2004
    #7
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.