Skip to content

Instantly share code, notes, and snippets.

@fusion5
Created October 9, 2011 19:03
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 fusion5/1274025 to your computer and use it in GitHub Desktop.
Save fusion5/1274025 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int countOnes (int n)
{
int r = 0;
while (n > 0) {
if (n % 10 == 1) {
r ++;
}
n = n / 10;
}
return r;
}
int main ()
{
int sum = 0;
for (int i = 1; i <= 100000000; i++)
{
sum += countOnes (i);
if (sum == i) {
cout << i << endl;
}
}
}
/*
$ time ./count
1
199981
199982
199983
199984
199985
199986
199987
199988
199989
199990
200000
200001
1599981
1599982
1599983
1599984
1599985
1599986
1599987
1599988
1599989
1599990
2600000
2600001
13199998
35000000
35000001
35199981
35199982
35199983
35199984
35199985
35199986
35199987
35199988
35199989
35199990
35200000
35200001
real 0m5.942s
user 0m5.936s
sys 0m0.000s
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment