[Question]
  [
Let's consider the following sequence:
$$9,8,7,6,5,4,3,2,1,0,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71...$$
This is the sequence of \$9\$'s complement of a number: that is, \$ a(x) = 10^d - 1 - x \$ where \$ d \$ is the number of digits in \$ x \$. ([A061601](https://oeis.org/A061601) in the OEIS).Your task is to add the first \$n\$ elements.
### Input
A number \$n∈[0,10000]\$.
### Output
The sum of the first \$n\$ elements of the sequence.
### Test cases
```
0 -> 0
1 -> 9
10 -> 45
100 -> 4050
1000 -> 408600
10000 -> 40904100
```
Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply. The shortest code in bytes wins.
  
[Answer]
# [R](https://www.r-project.org/), 37 bytes
```
f=function(n)sum(10^nchar(1:n-1)-1:n)
```
[Try it online!](https://tio.run/##K/r/P802rTQvuSQzP08jT7O4NFfD0CAuLzkjsUjD0CpP11BTF0hp/k/TMNTkAhIGEBJGwWkDzf8A "R – Try It Online")
**note**
I left out the check for 0 as I do not think it is part of the sequence as refered to A061601 in the OEIS reference. Adding the check would result in the exact same answer of Giuseppe, which would surely be preferable including the correct answer including 0.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 42 bytes
```
FromDigits[9-IntegerDigits@--n]~Sum~{n,#}&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n277360oP9clMz2zpDjaUtczryQ1PbUIwnfQ1c2LrQsuza2rztNRrlX7H1CUmVeioO@Qru9QbaBjqGMIJAzAGEIY1P7/DwA "Wolfram Language (Mathematica) – Try It Online")
-2 bytes from att
[Answer]
# [Julia 1.0](http://julialang.org/), ~~43~~ ~~38~~ 30 bytes
```
!n=n>0&&10^ndigits(~-n)-n+!~-n
```
[Try it online!](https://tio.run/##yyrNyUw0rPj/XzHPNs/OQE3N0CAuLyUzPbOkWKNON09TN09bEUj/L0ktLolPTixOLVawVYg20DHUMQQSBmAMIQxiuTT0FBHq9GxtgeosdUxMdUwMTA2AhIWZAYiyNDABqo/V1KuxKyjKzCvJyfsPAA "Julia 1.0 – Try It Online")
-5 dingledooper, -8 MarcMush
[Answer]
# [R](https://www.r-project.org/), 39 bytes
```
function(n)sum(10^nchar(1:n-1)-1:n)*!!n
```
[Try it online!](https://tio.run/##K/qfpmCj@z@tNC@5JDM/TyNPs7g0V8PQIC4vOSOxSMPQKk/XUFMXSGlqKSrm/S9OLCjIqdQwsDI00EnT5EoDqjQwgNEGmv8B "R – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 8 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
FNg°<N-O
```
[Try it online](https://tio.run/##yy9OTMpM/f/fzS/90AYbP13///8NDQA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfaVLfKZL7X83v/RDG2z8dP3/1@r8jzbQMdQxBBIGYAwhDGIB).
**Explanation:**
```
F         # Loop `N` in the range [0, (implicit) input):
 Ng       #  Push the length of `N`
   °      #  Take 10 to the power this length
    <     #  Decrease it by 1
     N-   #  Decrease it by `N`
       O  #  Take the sum of the values on the stack
          # (after which the last sum is output implicitly as result - 
          # or the implicit input if it was 0 and we never entered the loop)
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `r`, 8 bytes
No, I'm not Jo King. I found another use for the `r` flag!!!
> 
> It happens once in a blue moon on average
> 
> 
> 
Okay, yes, I said that
```
ʁƛL↵-‹;∑
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJyIiwiIiwiyoHGm0zihrUt4oC5O+KIkSIsIiIsIjEwIl0=)
[Flagless 9 bytes](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLKgcabTOKGtW4t4oC5O+KIkSIsIiIsIjEwIl0=)
## Explanation
So the `r` flag is basically a reverser, which reverses the order of the arguments. That makes it hard to use for long programs, but for short ones like this, it's useful sometimes.
```
ʁƛL↵-‹;∑
ʁ          create list with range(0, n)
 ƛ         open mapping lambda (with loop item n)
  L↵       push 10 to the length of n
    -      n - the latter (because it's reversed)
     ‹;    decrement by 1 and close lambda
       ∑   sum list
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
’æċ⁵_)S
```
A monadic Link accepting a non-negative integer that yields a non-negative integer.
**[Try it online!](https://tio.run/##y0rNyan8//9Rw8zDy450P2rcGq8Z/P//f0NDAA "Jelly – Try It Online")**
### How?
```
’æċ⁵_)S - Link: non-negative integer, n
     )  - for each i in [1..n] (if n is 0 this will yield []):
’       -   decrement -> i-1
   ⁵    -   10
 æċ     -   ceil i-1 to the nearest (positive integer) power of 10
    _   -   subtract i
      S - sum
 
```
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), ~~50~~ 44 bytes
Gave a fun recursive solution a go :)
edit: -6 bytes thanks to solid.py
```
f=lambda n:n and f(m:=n-1)-n+10**len(str(m))
```
[Try it online!](https://tio.run/##FcVBCsMgEAXQfU/xd5lJE1C6KYInKV1YjFSIP2Ld5PS2hQevnv198HavbYzk91BeMYCOCIxIUpznanXl1Zp53jfKpzcpqiMdDRmZeJgF9uefMU93AWrL7JIk64KN0U@YdHwB "Python 3.8 (pre-release) – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 79 bytes
```
g(n,t,s){t=--n/10;s=n%10+1;n=t*45+s*(19-s)/2+10*(t?s*g(t+1)+(10-s)*g(t)-90:0);}
```
[Try it online!](https://tio.run/##LU5BTsQwDDyTV1grVbKbRLUXOCwh8BDYA8rSKgeyaJMTVd9eHMHB4xmPxnbyS0r7vmBxzVVaW/S@TMKhxjIIWwkltvHh0dYR5eQrTUcrPGJ7reOCzQpZFNZ5V@RP/MQUtv3rIxek1eTSoKW3M0RYzR07caKgeHRyr5x7/QE7swUzX2@APZUjB8jwDDX/fF5nbImmf6o2qWct9aXfN9UzHoYL@BcYLu/l4PRkPjv9qHeiYDaz7b8 "C (gcc) – Try It Online")
[Answer]
# [Zsh](https://www.zsh.org/), 35 bytes
```
for ((x=$1;x--;s+=1e$#x+~x)):
<<<$s
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m724qjhjwYKlpSVpuhY3ldPyixQ0NCpsVQytK3R1rYu1bQ1TVZQrtOsqNDWtuGxsbFSKIUqhOpZGGxoYxEI5AA)
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 57 49 bytes
-8 bytes thanks to @mazzy!
```
0.."$args"-gt0|%{$s-=$_---("1e"+"$_".length)}
+$s
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/30BPT0klsSi9WEk3vcSgRrVapVjXViVeV1dXQ8kwVUlbSSVeSS8nNS@9JEOzlktbpfj///@GBgYA "PowerShell – Try It Online")
[Answer]
# [Behaviour](https://github.com/FabricioLince/BehaviourExolang), ~~36~~ 29 bytes
```
f=&#(a*&10^#(""+a)-1-a;>&a+b)
```
My own recently made esolang, so bear with me.
ungolfed and commented
```
f=&
    #(                  // deal with 0 case, convert nil into 0
      a * &             // generate array for n nine complements
        10^#(""+a)-1-a; // nine complements logic
      >&a+b             // reduce array by adding its items
    )
```
Test with:
```
f:0
f:1
f:10
```
[Answer]
# [Nibbles](http://golfscript.com/nibbles/index.html), ~~8.5~~ 6.5 bytes
```
+.,$-^~,`p-$~
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m708LzMpKSe1eMGCpaUlaboWa7X1dFR04-p0Egp0VeogYlCpBVsNuAy5DIGEARhDCAOIJAA)
*-2 bytes thanks to Dominic van Essen*
```
+.,$-^~,`p-$~
+             Sum
 .             for n in
  ,             range from 1 to
   $             input
    -          subtract
     ^~         10 to the power of
       ,         length of
        `p        convert to string
          - ~      subtract 1 from
           $        n
                n
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-mx`](https://codegolf.meta.stackexchange.com/a/14339/), 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
+1 byte to work around a bug in Japt.
```
aÓApUs l
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LW14&code=YdNBcFVzIGw&input=MTAw)
[Answer]
# [Python 3](https://docs.python.org/3/), 49 bytes
*-1 byte thanks to user `friddo`*
```
lambda n:sum(~i+10**len(str(i))for i in range(n))
```
[Try it online!](https://tio.run/##K6gsycjPM/6fZhvzPycxNyklUSHPqrg0V6MuU9vQQEsrJzVPo7ikSCNTUzMtv0ghUyEzT6EoMS89VSNPU/M/XCjaQEfBEIhAlAGEgJIGsVZcCkBQUJSZV6KRBjRIRyE1L8VWXUFd8z8A "Python 3 – Try It Online")
[Answer]
# [Dyalog APL](https://www.dyalog.com/), 17 bytes
```
+/⍳-⍨1-⍨10*≢∘⍕¨∘⍳
```
`⎕IO←0`.
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob6qn/6O2CQb/04Cktv6j3s26j3pXGIIJA61HnYsedcx41Dv10Aowvfn//7RDKwwUDBUMgYQBGEMIAwA)
[Answer]
# JavaScript, 47 bytes
```
f=n=>(t=10**`${n}`.length)*n+n*~n/2+~-t*~t/11+9
```
[Try it online!](https://tio.run/##LYxNDoIwFAav8i1c0PKAFn9Z1IsYEwhS0DSvBho2Rq5eJbqZWUwyj2Zupna8P0PG/tbFaA2bcxKMVlLWmxe/69x13IdBSE5ZLlyU6ZIFuYRC67SK1o9I5sbBW1wUQRNKwpawI@wJB8IRdCJU37Rm9cOf6irQep6863Ln@3VEsKuEiB8 "JavaScript (Node.js) – Try It Online")
There isn't any non-recursive implementation here. So maybe it is worth to include one. But it is longer than current answers...
If your language trunk divide result to int automatically, `+~-t*~t/11` could be changed into `-t*t/11`.
$$ t = 10^{1+\lfloor\log\_{10}n\rfloor} $$
$$ t\cdot n-\frac{n\cdot (n+1)}2-\frac{t^2-100}{11} $$
with special case where \$n=0\$
[Answer]
# [MATL](https://github.com/lmendo/MATL), 14 bytes
```
:q"10@Vn^q@-vs
```
[Try it online!](https://tio.run/##y00syfn/36pQydDAISwvrtBBt6z4/39DAwA "MATL – Try It Online")
[Answer]
# [Desmos](https://desmos.com/calculator), 49 bytes
```
f(k)=∑_{n=2}^k(10^{1+floor(log(n-1))}-n)+9-0^k9
```
Would be 44 bytes if we didn't have to deal with 0...
[Try It On Desmos!](https://www.desmos.com/calculator/hx3t6kc5n5)
[Try It On Desmos! - Prettified](https://www.desmos.com/calculator/hogm4nge2i)
## 48 bytes, port of [@tsh](https://codegolf.stackexchange.com/users/44718/tsh)'s [answer](https://codegolf.stackexchange.com/a/243275/96039)
```
t=10^{log(n+0^n)}10
f(n)=tn-(nn+n)/2-(tt-100)/11
```
[Try It On Desmos!](https://www.desmos.com/calculator/epcgwgznbc)
[Answer]
# [Raku](https://raku.org/), 32 bytes
```
0,|[\+] {{S:g/./9/-$_}($++)}...*
```
[Try it online!](https://tio.run/##K0gtyjH7n1up4FCsYPvfQKcmOkY7VqG6OtgqXV9P31JfVyW@VkNFW1uzVk9PT@u/tUJxYqWCkkq8gq0dUEu0SnyskkJafpGCjYGCoYIhkDAAYwhhYPcfAA "Perl 6 – Try It Online")
This is an expression for the infinite sequence of sums.
* `{ ... } ... *` is a lazy, infinite sequence, where the brackets enclose an expression that generates each sequence element.
* `$++` postincrements an anonymous state variable each time the generating function is called, producing the numbers 0, 1, 2, .... Each time, that number is passed to the anonymous function enclosed in the inner brackets. (That saves us having to declare a variable to hold the counter value.)
* `S:g/./9/` converts every digit in the counter to `9`.
* `- $_` subtracts the counter from the previous value.
* `[\+]` produces the sequence of partial sums from the original sequence.
* `0, |` pastes a zero onto the front of the sequence.
[Answer]
# [J-uby](https://github.com/cyoce/J-uby), 32 bytes
```
:*|:sum+(-[S|:+@|:**&10,:~]|+:+)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m700K740qXLBIjfbpaUlaboWNxWstGqsiktztTV0o4NrrLQdaqy0tNQMDXSs6mJrtK20NaHqNAoU3KINYrlAlCGUgnENEAyDWIj6BQsgNAA)
## Explanation
```
:* | :sum + ( -[ S | :+@ | :** & 10, :~ ] | +:+ )
:* |                                               # 0...input
     :sum + (                                   )  # Sum by
              -[                   ,    ] | +:+    #   Sum of
                 S | :+@ | :** & 10                #   10 to the power of the number of digits
                                     :~            #   and two's complement
```
[Answer]
# [Thunno](https://github.com/Thunno/Thunno), \$ 11 \log\_{256}(96)\approx \$ 9.05 bytes
```
eDL10@1-_ES
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72sJKM0Ly9_wYKlpSVpuharU118DA0cDHXjXYMhIlCJBYsNDQwgTAA)
#### Explanation
```
   # Implicit input 
e  # Map over range(input):
DL #  Duplicate and push the length
10 #  Push ten
@  #  Push 10 ** length
1- #  Subtract one
_  #  Subtract the loop variable
ES # After the map, sum
   # Implicit output 
```
[Answer]
# [FunStack](https://github.com/dloscutoff/funstack) alpha, 60 bytes
```
Cons 9 Reverse From0 over flatmap Times 9 Pow 10 #N Sum Take
```
Try it at [Replit](https://replit.com/@dloscutoff/funstack)!
### Explanation
First, we construct the infinite list `[9,8,7,6,5,4,3,2,1,0,89,88,87,...]`:
```
#N
```
Natural numbers starting from 0.
```
Times 9 Pow 10
```
Take 10 to the power of each number, then multiply by 9.
```
Reverse From0 over flatmap
```
Create a function that takes a number, generates the range from 0 to that number (exclusive), and reverses it. Then map that function to each number in the above list and flatten the resulting list of lists.
(What we're doing here is really just function composition, but `over` is fewer bytes than `compose` and does the same thing in this case.)
```
Cons 9
```
Stick a 9 on the front of the list.
Now this value gets appended to the program's argument list (which previously contained the input number, N), and we apply the following functions:
```
Take
```
Take the first N values from the infinite list.
```
Sum
```
Sum them.
[Answer]
# JavaScript, 35 bytes
```
f=n=>n&&f(--n)+10**`${n}`.length+~n
```
[Try it online!](https://tio.run/##FcsxCoAgFADQq/whQhPDoCEwO4sRaYZ8I8Ul6upW05vePuc5Lqc7Es9DKUahmrCuDeEcKetE0@jqwlu3fkWbNvZgMeEE4kCBkOBgVNB/MkZhCRiDX1sfLDHkv45SWV4 "JavaScript (V8) – Try It Online")
[Answer]
# [J](http://jsoftware.com/), 24 bytes
```
+/@((10&^@#@":->:)"0@i.)
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/tfUdNDQMDdTiHJQdlKx07aw0lQwcMvU0/2tycaUmZ@QrpCkYwBiGcAZCyACZicI24PoPAA "J – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 42 bytes
```
f=lambda n:n and-~~n+10**len(`n-1`)+f(n-1)
```
[Try it online!](https://tio.run/##FcVNCoAgFEbReat4Q38StGHQSiLQMEmoT5EmTdy62eXCye9zJkytheVy9@4dYQY5eFUrpNFCXAeYhTKWy8C6vIVUKFIErXok0//RepsH6uUS8bDAIuftAw "Python 2 – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 39 bytes
```
->n{n.times.sum{|x|10**x.to_s.size+~x}}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y6vOk@vJDM3tVivuDS3uqaixtBAS6tCryQ/HiiSWZWqXVdRW/u/QCEt2iCWC0QZQikY1wDBMIj9DwA "Ruby – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 12 bytes
```
Ｉ↨¹ＥＮ⁻Ｉ×Ｌι9ι
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEwymxOFXDUEfBN7FAwzOvoLTErzQ3KbVIQxMolJlXWgxRFZKZm1qs4ZOal16SoZEJlFOyVNIEUpmaIGD9/7@hgYHBf92yHAA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
    Ｎ           Input as a number
   Ｅ            Map over implicit range
          9     Literal string `9`
       ×        Repeated by
         ι      Current value
        Ｌ       Length (of string representation)
      Ｉ         Cast to integer
     ⁻          Subtract
           ι    Current value
 ↨¹             Take the sum
Ｉ               Cast to string
                Implicitly print
```
(`Sum()` won't work for an input of `0`.)
[Answer]
# [Burlesque](https://github.com/FMNSSun/Burlesque), 19 bytes
```
-.{Jln'9j.*j|-}GZ++
```
[Try it online!](https://tio.run/##SyotykktLixN/V@tpGunZG2tG1uU@V9Xr9orJ0/dMktPK6tGt9Y9Slv7f214zn8DBV07BQMuQxBlyWUI5pqYAhkQloGpAYgN5ViYGUC4UL6lgQmQBwA "Burlesque – Try It Online")
```
-.   # Decrement (Burlesque runs 0..N inclusive, need to exclude)
{
 J   # Duplicate
 ln  # Number of digits
 '9  # Character 9
 j   # Reorder stack
 .*  # Repeat 9 n_digits times as string
 j   # Reorder stack
 |-  # String subtract (parse and subtract)
}GZ  # Generate for range
++   # Sum
```
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 30 bytes
```
n->sum(i=0,n-1,10^#Str(i)-1-i)
```
[Try it online!](https://tio.run/##LYdBCoAwDAS/EuqlgQbSB9hPeBQFL0pAQ6j14OurRWGY2bUlC21WV@irUjqvw0vPQSmGyHM3lOwFKZJgXcz22ytQAsui5Z2uHQerV8QAIweILy386TdPWB8 "Pari/GP – Try It Online")
]