<?PHP /* Source to solve question 17 on the * Google Labs Aptitude Test: * * 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? */ function f ($l,$f,$n) { /* This is the function described in the question * with a few optimizations. * * return = f(startval,start,number) * * startval is for resuming where one left off, * it is the value of the ones from 0 to start. * * start is for resuming where one left off, * it is the number where one left off. * * number is the exact same as described in the * question. */ $num = $l; for ($i = $f;$i <= $n;$i++) { str_replace('1','',$i,$c); $num = $num + $c; } return $num; } $verbose = 1; $n = 2; $last = 1; $p = 1; while (1) { if ($verbose) { echo 'Checking '.$n.'.. '; } $current = f($last,$n,$n); if ($verbose) { echo $current.'.. '; } if ($current == $n) { if ($verbose) { echo 'Success!'."\n"; } else { echo $n."\n"; } die(); } else { if ($verbose) { echo 'Failure!'."\n"; } } $p = $n; $n++; $last = $current; }?>