division by 7 efficiently ???

Jesse Chounard jessechounard at gmail.com
Fri Feb 2 10:31:08 EST 2007


On 31 Jan 2007 19:13:14 -0800, krypto.wizard at gmail.com
<krypto.wizard at gmail.com> wrote:
> Its not an homework. I appeared for EA sports interview last month. I
> was asked this question and I got it wrong. I have already fidlled
> around with the answer but I don't know the correct reasoning behind
> it.

I think this works:

>>> def div7 (N):
...     return ((N * 9362) >> 16) + 1
...
>>> div7(7)
1
>>> div7(14)
2
>>> div7(700)
100
>>> div7(70000)
10000

The coolest part about that (whether it works or not) is that it's my
first Python program.  I wrote it in C first and had to figure out how
to convert it.  :)

Jesse



More information about the Python-list mailing list