[Question]
  [
I need to prepare digits made of cardboard to display some number ([example](https://i.stack.imgur.com/eWidM.jpg)). I don't know beforehand which number I should display - the only thing I know is that it's not greater than `n`.
How many cardboard digits should I prepare?
Example: `n = 50`
To display any number in the range 0...50, I need the following digits:
1. A zero, for displaying the number 0, or any other round number
2. Two copies of digits 1, 2, 3 and 4, for displaying the corresponding numbers
3. One copy of digits 5, 6, 7 and 8, for the case they appear as least significant digit in the number
4. The digit 9 is never needed, because I can use the inverted digit 6 instead
Total: 13 digits
Test cases (each line is a test case in the format "input; output")
```
0 1
1 2
9 9
11 10
50 13
99 17
100 18
135 19
531 22
1000 27
8192 34
32767 38
```
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 bytes
```
‘ḶDœ|/ḟ9L
```
[Try it online!](https://tio.run/##AR8A4P9qZWxsef//4oCY4bi2RMWTfC/huJ85TP///zMyNzY3 "Jelly – Try It Online")
## How it works
```
‘ḶDœ|/ḟ9L
‘Ḷ         [0,1,...,n]
  D        convert each to list of its digits
   œ|/     fold by multiset union
      ḟ9   remove 9
        L  length
```
[Answer]
# [Python 2](https://docs.python.org/2/), 49 bytes
```
lambda n:9*len(`n`)-9+(n*9+8)/10**len(`n`)+(n<10)
```
[Try it online!](https://tio.run/##PYrBDoIwEETP7FfssQXUbhuEEvsnJlIiBBKoDWKIX4@rB@cwk7x58b0Oj6D33l33yc/t3WOobTp1QTShkQebiZDarJInUukfM7yQkvs2jFOHVEPi89bNPooxrPnit9sY4msV8viM08grIVlcLzxvXNhBn@OSY8vtXLsrJCDUoNGARQukvkVICgr@GFqkEsgUSBYKw65mSSnUJRhdnks0FVS/YGE@ "Python 2 – Try It Online")
A clumsy arithmetical formula. Assume that `n` fits within an `int` so that an `L` isn't appended.
Thanks to Neil for saving 5 bytes by pointing out that 9's being unused could be handled by doing `n*9+8` instead of `n*9+9`, so that, say, `999*9+8=8999` doesn't roll over to 9000.
[Answer]
# [Haskell](https://www.haskell.org/), ~~117~~ ~~114~~ ~~108~~ ~~95~~ ~~89~~ ~~88~~ ~~87~~ ~~84~~ ~~82~~ 63 bytes
*6 bytes saved thanks to Laikoni*
*~~1~~ ~~4~~ 6 bytes saved thanks to nimi*
```
g x=sum[maximum[sum[1|u<-show y,d==u]|y<-[0..x]]|d<-['0'..'8']]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P12hwra4NDc6N7EiMxdIg9iGNaU2usUZ@eUKlToptralsTWVNrrRBnp6FbGxNSlAprqBup6euoV6bOz/3MTMPCsrT38NTS4Q07agKDOvRCVdwdDI0vg/AA "Haskell – Try It Online")
[Answer]
# Mathematica, 49 bytes
```
Tr@Delete[Max~MapThread~DigitCount@Range[0,#],9]&
```
[Answer]
## Batch, 67 bytes
```
@if %1 geq 10%2 %0 %1 0%2 -~%3
@cmd/cset/a(%1*9+8)/10%2+9*%30+!%30
```
In the standard formulation of this problem, you need separate `6` and `9` digits, but you're not required to display `0`. As the maximum value `n` required increases, the number of required numerals increases every time you reach a repdigit (because you don't quite have enough of that numeral) and every time you reach a power of `10` (when you need an extra zero). In total each power of `10` needs `10` more numerals than the previous one, which can be caluclated as `floor(log10(n))*10`. For values of `n` between powers of 10, the number of intermediate repdigits can then be calculated as `floor(n/((10**floor(log10(n))*10-1)/9))` or alternatively `floor(n*9/(10**floor(log10(n))*10-1))`.
I calculate `floor(log10(n))` by means of the loop on the first line. Each time, `%2` gains an extra `0` and `%3` gains an extra `-~`. This means that `10%2` is `10*10**floor(log10(n))` and `%30` is `floor(log10(n))`.
The duplication of `6` and `9` has two effects: firstly, there are only `9` numerals required for each power of `10`, and secondly the repdigit detection needs to ignore the `9` repdigits. Fortunately as they are one less than a power of 10 this can be achieved by tweaking the formula to result in `floor((n*9+8)/(10**floor(log10(n))*10))`.
Dealing with the zero is reasonably simple: this just requires an extra numeral when `n<10`, i.e. `floor(log10(n))==0`.
[Answer]
## JavaScript (ES6), ~~60~~ 53 bytes
```
f=(n,i=9)=>n>(i%9+1+"e"+(i/9|0))/9-1?1+f(n,-~i):n>9^1
```
A sort of hacky recursive solution. This generates the numbers which require adding a digit:
```
1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 22, 33, 44, 55, 66, 77, 88, 100, 111, 222, ...
```
and then counts how many are less than the input. By a happy miracle, removing the digit `9` actually *removes* several bytes from the function, because the sequence can then be generated like so (assuming integer division):
```
1e1 / 9 = 1, 2e1 / 9 = 2, ..., 8e1 / 9 = 8, 9e1 / 9 = 10, 1e2 / 9 = 11, 2e2 / 9 = 22, ...
```
We do have to take into account the fact that numbers under 10 still require the zero, but this is as simple as adding `n > 9 ? 0 : 1` to the result.
### Test cases
```
let f=(n,i=9)=>n>(i%9+1+"e"+(i/9|0))/9-1?1+f(n,i+1):n>9^1;
[0, 1, 9, 11, 50, 99, 100, 135, 531, 1000, 8192, 32767].map(n => console.log(f(n)));
```
[Answer]
# Mathematica, 83 bytes
```
v=DigitCount;s=v@0;(Table[s[[i]]=v[j][[i]]~Max~s[[i]],{i,10},{j,#}];s[[9]]=0;Tr@s)&
```
[Answer]
# [Python 3](https://docs.python.org/3/), 75 bytes
```
lambda n:sum(max(str(j).count(str(i))for j in range(n+1))for i in range(9))
```
[Try it online!](https://tio.run/##K6gsycjPM/6fZhvzPycxNyklUSHPqrg0VyM3sUKjuKRII0tTLzm/NK8EzMnU1EzLL1LIUsjMUyhKzEtP1cjTNoSIZSLELDU1/xcUZQL1pGkYaGpywdiGSGxLZHFkCVNkHZYoyoxNkdUZI@syNDBA1mdsZG5mDnQFAA "Python 3 – Try It Online")
[Answer]
# [PHP](https://php.net/), 60 bytes
```
<?=count(preg_grep('#^100+$|^([0-8])\1*$#',range(0,$argn)));
```
[Try it online!](https://tio.run/##K8go@G9jX5BRwKWSWJSeZ6tkbGRuZq5kzWVvBxS3Tc4vzSvRKChKTY9PL0ot0FBXjjM0MNBWqYnTiDbQtYjVjDHUUlFW1ylKzEtP1TDQARuiqalp/f8/AA "PHP – Try It Online")
]