A better solution is recursive. If you know the number of zeros up to n, then the number of zeros up to 10n + 9 can be computed easily; each zero is repeated ten times, plus there are an additional n + 1 zeros in the least significant position. Then it is only necessary to subtract the 9 – c zeros for those numbers that don’t end in 0:

The input is considered left-to-right, z is the number of zeros in the portion of the input that has already been considered, n is the number that has so far been considered (the actual number itself, so after two recursive calls on the input number 1000000000000, n is 10), and f is the accumulating result.

Because the number of zeros grows quickly, and n may be large, it is convenient to provide a version of the counting function that returns the result modulo some constant: