Skip to content

Instantly share code, notes, and snippets.

@duartenina
Created July 11, 2014 14:16
Show Gist options
  • Save duartenina/4081821e5796773e37f1 to your computer and use it in GitHub Desktop.
Save duartenina/4081821e5796773e37f1 to your computer and use it in GitHub Desktop.
Number trick (always gets 13)
def get_max_exponent (n):
n_copy = int(n)
n_max = 0
while True:
div = 10**n_max
if (n_copy//div)*div == 0:
break
n_max += 1
return n_max - 1 #To get the break condition, it's needed the first exponent larger than the number
def get_digits (n):
sum = []
n_copy = int(n)
n_max = get_max_exponent (n_copy)
for exp in xrange(n_max,0,-1):
div = 10**exp
parcel = n_copy//div
sum.append(parcel)
n_copy -= div*parcel
sum.append(n_copy)
return sum
def sum_list (list):
sum = 0
for member in list:
sum += member
return sum
def f (n):
sum = 9*n
i = 0
while sum > 10:
digits = get_digits(sum)
sum = sum_list(digits)
sum += 4
return sum
if __name__ == '__main__':
for i in xrange(1,110):
print i, i*9, get_digits(i*9), f(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment