division by 7 efficiently ???

MRAB google at mrabarnett.plus.com
Tue Feb 6 16:26:29 EST 2007


On Feb 6, 3:29 pm, garri... at gmail.com wrote:
> On Feb 1, 8:25 pm, "Krypto" <krypto.wiz... at gmail.com> wrote:
>
> > The correct answer as told to me by a person is
> > (N>>3) + ((N-7*(N>>3))>>3)
> > The above term always gives division by 7
>
> Does anybody else notice that this breaks the spirit of the problem
> (regardless of it's accuracy)? 'N-7' uses the subtraction operator,
> and is thus an invalid solution for the original question.
>
[snip]
Instead of subtraction you can use complement-and-add: N + ~7 + 1
(that's a tilde '~' not a minus '-').




More information about the Python-list mailing list