Created
January 6, 2013 04:25
-
-
Save anonymous/4465193 to your computer and use it in GitHub Desktop.
use len(str(x)) to find the length of a number
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
"""Compares two differing methods of determining a number's length.""" | |
import math | |
def using_log(number): | |
"""Using math.log to figure out the length of a number.""" | |
return math.floor(math.log(number, 10)) + 1 | |
def using_len(number): | |
"""Converts the number to a string and determines length through len().""" | |
return len(str(number)) | |
def find_weird_maths(): | |
"""Finds where the levee breaks.""" | |
digit = 9 | |
number = digit | |
for i in range(1, 500): | |
if using_log(number) != using_len(number): | |
print "9's x %d differs logrithmic v string." % using_len(number) | |
number = int(str(digit) * i) | |
if __name__ == "__main__": | |
find_weird_maths() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment