18/07/23 18:30:43.78 B1Q5jsGg.net
/*
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 number n such that f(n)=n.
*/
# include <stdio.h>
# define MAX_N 10^6
# define MAX_CHAR 7
int f0(int n){ /* 1 in number n */
int i;
int count = 0;
char num[MAX_CHAR];
sprintf(num,"%d",n); /* integer to char */
for(i = 0 ; i < MAX_CHAR; i++){
if(num[i] == '1') count++; /* if char==1, count++ */
if(num[i] == '\0') break; /* end of string */
}
return count;
}
int main(){
int fn = 0; /* sum of f0 from 1 to n */
int n;
for(n = 1; n < MAX_N ; n++){
fn = fn + f0(n);
if(fn == n){
printf("f(%d) = %d\n",n,fn);
}
}
return 0;
}