Skip to content

Instantly share code, notes, and snippets.

@knutwalker
Created May 9, 2012 22:06
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 knutwalker/2649258 to your computer and use it in GitHub Desktop.
Save knutwalker/2649258 to your computer and use it in GitHub Desktop.
Python: Solve GLAT question 17
#!/usr/bin/env python
"""
A brute-force approach to solve the seventeenth quesiton of GLAT
More about GLAT: http://googleblog.blogspot.de/2004/09/pencils-down-people.html
The questoin is as follows:
17. Consider a function which, for a given whole number n,
returns the number of ones required when writing out all numbers
between 0 and n. For example, f(13)=6. Notice that f(1)=1.
What is the next largest n such that f(n)=n?
"""
from itertools import count
ones = 0
for x in count(1):
ones += str(x).count('1')
if ones == x and x > 1:
print "f({0}) = {1}".format(x, ones)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment