Skip to content

Instantly share code, notes, and snippets.

@cpylua
Created March 16, 2012 14:58
Show Gist options
  • Save cpylua/2050431 to your computer and use it in GitHub Desktop.
Save cpylua/2050431 to your computer and use it in GitHub Desktop.
count total number of ones in 1...n
def f(n):
cache = [0]
cnt = 0
for i in xrange(1, n+1):
remainder = i % 10
quotient = i / 10
m = cache[quotient]
if remainder == 1:
m = m + 1
cache.append(m)
cnt = cnt + m
return cnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment