division by 7 efficiently ???

Michael Yanowitz m.yanowitz at kearfott.com
Thu Feb 1 15:51:19 EST 2007


  I think it is off by 1 in small numbers, off by a little more with large
numbers:
>>> def div7 (N):
...    return (N>>3) + ((N-7*(N>>3))>>3)
...
>>> div7 (70)
9
>>> div7 (77)
10
>>> div7 (700)
98
>>> div7 (7)
0
>>> div7 (10)
1
>>> div7 (14)
1
>>> div7 (21)
2
>>> div7 (700)
98
>>> div7 (7000)
984

Michael Yanowitz

-----Original Message-----
From: python-list-bounces+m.yanowitz=kearfott.com at python.org
[mailto:python-list-bounces+m.yanowitz=kearfott.com at python.org]On Behalf
Of Krypto
Sent: Thursday, February 01, 2007 3:25 PM
To: python-list at python.org
Subject: Re: division by 7 efficiently ???


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

--
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list