Created
July 15, 2015 18:50
-
-
Save gnuton/99f128f7587832f6a9b9 to your computer and use it in GitHub Desktop.
calculate logarithm in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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