Skip to content

Instantly share code, notes, and snippets.

Created January 6, 2013 04:25
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 anonymous/4465193 to your computer and use it in GitHub Desktop.
Save anonymous/4465193 to your computer and use it in GitHub Desktop.
use len(str(x)) to find the length of a number
"""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