public
anonymous / gist:4465193
Created

use len(str(x)) to find the length of a number

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
"""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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.