Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: divide by 7 efficiently

  1. #1
    Join Date
    Jan 2006
    Location
    Florida
    Beans
    377
    Distro
    Ubuntu 6.06 Dapper

    divide by 7 efficiently

    How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer.

  2. #2
    Join Date
    Nov 2006
    Beans
    Hidden!

    Re: divide by 7 efficiently

    wait i forgot we can't use /

    LOL

  3. #3
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: divide by 7 efficiently

    Well, bit shifts work great for fast power of two division/multiplication, such as...

    Code:
    x>>1  = x/2
    x>>2  = x/4
    x>>3  = x/8
    x>>4  = x/16
    x>>5  = x/32
    x>>6  = x/64
    x>>7  = x/128
    x>>8  = x/256
    x>>9  = x/512
    x>>10 = x/1024
    
    x<<1  = x*2
    x<<2  = x*4
    x<<3  = x*8
    x<<4  = x*16
    x<<5  = x*32
    x<<6  = x*64
    x<<7  = x*128
    x<<8  = x*256
    x<<9  = x*512
    x<<10 = x*1024
    But only power of two numbers, I don't know of any way that you can use them for non-power of twos (because of how they work).

    0001 = 1
    (0001 << 1) = 0010 = 2
    (0010 << 1) = 0100 = 4
    (0100 >> 2) = 0001 = 1

    Bit shifts only work for integer numbers too.

  4. #4
    Join Date
    Aug 2006
    Location
    Belgium
    Beans
    Hidden!
    Distro
    Xubuntu 8.04 Hardy Heron

    Re: divide by 7 efficiently

    But you can use approximations that way.

    Code:
    1/7 = 0.142857
    
    Now 1/8 + 1/64 = 0.140625
    Or
    1/8+1/64+1/512 = 0.142578

  5. #5
    Join Date
    Jan 2006
    Location
    Florida
    Beans
    377
    Distro
    Ubuntu 6.06 Dapper

    Re: divide by 7 efficiently

    I think your solution is the closest I can get.

    Thanks

  6. #6
    Join Date
    Dec 2006
    Beans
    34

    Re: divide by 7 efficiently

    If you want to divide n by 7 you could do a binary search for the x that satisfies x*7 = n. Binary search requires being able to divide by 2, but that's easy with bitshifting (x/2 = x>>1)
    Last edited by Klipt; February 1st, 2007 at 07:54 PM.

  7. #7
    Join Date
    Aug 2005
    Location
    The Local Group
    Beans
    631

    Re: divide by 7 efficiently

    There are also recursive algorithms based on binary representation. For example, think of how you might get x/7 and x%7 if you had (x/2)/7 and (x/2)%7 from a recursive call. The way I'm thinking of traditionally requires some subtraction, but since your divisor is always 7, you can get around it.

    It might help to think of it this way: (a/b)*b + (a%b) == a.

  8. #8
    Join Date
    Feb 2006
    Location
    Finland
    Beans
    189
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: divide by 7 efficiently

    Or to be simple, multiply by 0.142857

  9. #9
    Join Date
    Nov 2006
    Beans
    Hidden!

    Re: divide by 7 efficiently

    ^ yeah but what's the point of that. at some point you used a / somewhere.

    how about efficient division by N without using / or -

  10. #10
    Join Date
    Aug 2006
    Beans
    278

    Re: divide by 7 efficiently

    int32_t result = (int32_t)(((int64_t)x * 613566757U) >> 32)

    This is (at least) exact for values smaller than 2^24. afterwards you can have a result thats 1 lower as the exact calculation (its still exact for most values).

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •