Re: division by 7 without using division operator



krypto.wizard@xxxxxxxxx wrote:

Last month I appeared for an interview with EA sports and they
asked me this question.

How would you divide a number by 7 without using division operator?

I did by doing a subtraction and keeping a counter that kept a tab
on how many times I subtracted.

Later, the EA sport guy told me that of course there are can be
better technique by using bit operator.

Since 7 has a binary representation 111, my guess is that a left
shift operation of 3 bits should give the answer, but I couldn't
get it to work.

If you are multiplying (not dividing) then:
n = 8 * n - n;
or
n = (n << 3) - n; /* The latter only for unsigned n. */

otherwise he was asking you to build a division routine.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


.



Relevant Pages

  • Re: division by 7 efficiently
    ... How would you divide a number by 7 without using division operator? ... I did by doing a subtraction and keeping a counter that kept a tab on ... could call it the "caveman" method, ...
    (comp.programming)
  • Re: division by 7
    ... How would you divide a number by 7 without using division operator? ... I did by doing a subtraction and keeping a counter that kept a tab on ... (that is actually a solution to there question if that was the exact ...
    (sci.math)
  • Re: division by 7
    ... How would you divide a number by 7 without using division operator? ... I did by doing a subtraction and keeping a counter that kept a tab on ... So to divide by 7: shift left 3, then again, then again, etc. ...
    (sci.math)
  • division by 7 efficiently
    ... Last month I appeared for an interview with EA sports and they asked ... How would you divide a number by 7 without using division operator? ... I did by doing a subtraction and keeping a counter that kept a tab on ...
    (comp.programming)
  • division by 7efficiently
    ... Last month I appeared for an interview with EA sports and they asked ... How would you divide a number by 7 without using division operator? ... I did by doing a subtraction and keeping a counter that kept a tab on ...
    (comp.theory)