Skip to content

Instantly share code, notes, and snippets.

@gnuton
Created July 15, 2015 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnuton/99f128f7587832f6a9b9 to your computer and use it in GitHub Desktop.
Save gnuton/99f128f7587832f6a9b9 to your computer and use it in GitHub Desktop.
calculate logarithm in python
# complexity: logarihmic! :D
def log2(n):
result=0
while n > 1:
n//=2
result +=1
return result
import unittest
import math
class log2Test(unittest.TestCase):
def setUp(self):
pass
def run1(self):
for i in xrange(100):
assert log2(i) == int(math.log(i,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment