Created
May 9, 2012 22:06
-
-
Save knutwalker/2649258 to your computer and use it in GitHub Desktop.
Python: Solve GLAT question 17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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