[Question]
  [
## The sequence
Given an integer \$n>0\$, we define \$a(n)\$ as the lowest positive integer such that there exists exactly \$n\$ positive integers smaller than \$a(n)\$ whose sum of digits is equal to the sum of the digits of \$a(n)\$.
*Edit: this sequence has since been published as [A332046](https://oeis.org/A332046)*
## First terms
```
  n | a(n) | sum of digits | matching integers
----+------+---------------+------------------------------------------------------
  1 |  10  |       1       | [1]
  2 |  20  |       2       | [2, 11]
  3 |  30  |       3       | [3, 12, 21]
  4 |  40  |       4       | [4, 13, 22, 31]
  5 |  50  |       5       | [5, 14, 23, 32, 41]
  6 |  60  |       6       | [6, 15, 24, 33, 42, 51]
  7 |  70  |       7       | [7, 16, 25, 34, 43, 52, 61]
  8 |  80  |       8       | [8, 17, 26, 35, 44, 53, 62, 71]
  9 |  90  |       9       | [9, 18, 27, 36, 45, 54, 63, 72, 81]
 10 | 108  |       9       | [9, 18, 27, 36, 45, 54, 63, 72, 81, 90]
 .. |  ..  |      ..       | ..
 20 | 216  |       9       | [9, 18, 27, 36, 45, 54, 63, 72, 81, 90, ..., 207]
 .. |  ..  |      ..       | ..
 30 | 325  |      10       | [19, 28, 37, 46, 55, 64, 73, 82, 91, 109, ..., 316]
 .. |  ..  |      ..       | ..
 40 | 442  |      10       | [19, 28, 37, 46, 55, 64, 73, 82, 91, 109, ..., 433]
 .. |  ..  |      ..       | ..
 50 | 560  |      11       | [29, 38, 47, 56, 65, 74, 83, 92, 119, 128, ..., 551]
```
## More examples
```
a(13) = 135
a(17) = 171
a(42) = 460
a(57) = 660
a(81) = 1093
a(82) = 1128
a(118) = 1507
a(669) = 9900
a(670) = 10089
a(1000) = 14552
a(5000) = 80292
a(10000) = 162085
```
As a side note, you may not assume that the sum of the digits may only increase. For instance, the sum of the digits for \$a(81)\$ is \$1+0+9+3=13\$, while the sum of the digits for \$a(82)\$ is \$1+1+2+8=12\$.
## Rules
* You may either:
	+ take a 1-indexed integer \$n\$ and return \$a(n)\$
	+ take a 0-indexed integer \$n\$ and return \$a(n+1)\$
	+ take a positive integer \$n\$ and return the \$n\$ first terms
	+ print the sequence indefinitely
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'")!
  
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 12 bytes
```
f}hQ_XsjT;H1
```
[Try it online!](https://tio.run/##K6gsyfj/P602IzA@ojgrxNrD8P9/IwMA "Pyth – Try It Online")
* `f`: Count up from 1 to find the first number such that:
* `X ... H1`: Increment or insert the value 1 into the dictionary H at index
* `sjT;`: sum of base ten digits of current number
* `_`: values of dictionary
* `}hQ`: check whether input + 1 is contained within.
The first time this is true, exactly input numbers must have the same digit sum as the current number, for the first time.
[Answer]
# [Gaia](https://github.com/splcurran/Gaia), ~~13~~ 10 bytes
```
1⟨┅Σ¦ṅC=⟩#
```
[Try it online!](https://tio.run/##S0/MTPz/3/DR/BWPprSeW3xo2cOdrc62j@avVAaKGgMA "Gaia – Try It Online")
Straightforward implementation.
## Explanation:
```
1⟨	⟩#	| find the first 1 positive integers N where:
      C		| the count of
     ṅ		| the digital sum d(N)
  ┅Σ¦		| in the list [d(1)..d(N-1)]
       =	| is equal to the (implicit) input
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 bytes
```
D€§ċṪ$=ʋ1#
```
[Try it online!](https://tio.run/##y0rNyan8/9/lUdOaQ8uPdD/cuUrF9lS3ofJ/60cNcxR07RQeNcy1PtzOZWxwuB2oJvI/AA "Jelly – Try It Online")
A monadic link taking an integer \$n\$ and returning \$a(n)\$. 
## Explanation
```
       ʋ1# | Find the first integer x where the following is true;
D€         | - Digits of 1..x
  §        | - Sum each
   ċṪ$     | - Count the number equal to the tail (after popping tail)
      =    | - Equal to (implicit original argument to link)
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 10 bytes
```
∞.Δ1Ÿ1öć¢Q
```
[Try it online!](https://tio.run/##AR0A4v9vc2FiaWX//@KIni7OlDHFuDHDtsSHwqJR//8xMw "05AB1E – Try It Online")
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 27 bytesSBCS
```
{⌈⌿+\∘.=⍨1⊥10⊥⍣¯1⍳20×⍵}⍳+∘1
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///v/pRT8ejnv3aMY86ZujZPupdYfioa6mhAZB41Lv40HrDR72bjQwOT3/Uu7UWyNQGqjL8n/aobcKj3r5HfVM9/R91NR9ab/yobSKQFxzkDCRDPDyD/6cdWqGgAdRgaKCpY2SgYGygYGKgYGqgYGGoYGEEAA "APL (Dyalog Unicode) – Try It Online")
### How it works
```
{⌈⌿+\∘.=⍨1⊥10⊥⍣¯1⍳20×⍵}⍳+∘1  ⍝ Right argument: n
{                     }⍳+∘1  ⍝ Find the first index of n+1 from...
 ⌈⌿+\∘.=⍨1⊥10⊥⍣¯1⍳20×⍵       ⍝ ... the list of cumulative count of own digit sum (sort of)
                  20×⍵         ⍝ Practical upper bound 20×n (could also use theoretical 10*⍵)
                 ⍳             ⍝ Range: 1..20×n
           10⊥⍣¯1  ⍝ Extract decimal digits from each
         1⊥        ⍝ Sum the digits of each number
     ∘.=⍨    ⍝ Self outer product by equality
   +\        ⍝ Cumulative sum in row direction; cumulative counts of own digit sum
 ⌈⌿          ⍝ Maximum in column direction; cumulative maximum of cumulative count
```
[Answer]
# [Python 2](https://docs.python.org/2/), 71 bytes
```
f=lambda n,x=[0]:x.count(x[-1])+~n and-~f(n,x+[sum(map(int,`len(x)`))])
```
[Try it online!](https://tio.run/##DcpNCoMwEAbQfU/xbcQZjMWfnWAvIgHTxrQBnYhVSDdePfWt3/rbP0GalFw/m@VpDUTFfqh0F@@vcMhOcShrzcUpMGLL09EViuF7LLSYlbzsapwnocgjs@bkwgYPL9iMvCeqFdqKuxuwbtdFnjUW5QNZa3NkIK/gyDOnPw "Python 2 – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 16 bytes
```
Ｗ⁻№υΣＬυθ⊞υΣＬυＩＬυ
```
[Try it online!](https://tio.run/##S85ILErOT8z5/788IzMnVUHDNzOvtFjDOb80r0SjVEchuDRXwyc1L70kQ6NUU1NTR6FQU1MhoLQ4A4ukNVdAUSZQm3NicQmK@P//Fob/dctyAA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｗ⁻№υΣＬυθ
```
Count the number of entries in the predefined list that are equal to the digital sum of the length of the list and repeat while that does not equal the input...
```
⊞υΣＬυ
```
... append the digital sum of the length of the list to the list.
```
ＩＬυ
```
Output the length of the list.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 55 bytes
```
->n,r=0,*w{w<<k=(r+=1).digits.sum until w.count(k)>n;r}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y5Pp8jWQEervLrcxibbVqNI29ZQUy8lMz2zpFivuDRXoTSvJDNHoVwvOR/I0sjWtMuzLqr9X6CQFm0YywWijCCUMZQygNCmUNrQwMAg9j8A "Ruby – Try It Online")
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~68~~ ~~67~~ 63 bytes
~~-1~~ -5 byte thanks Arnauld
```
n=>(F=z=>F[g=eval([...z+''].join`+`)]^n?F(-~z,F[g]=-~F[g]):z)``
```
[Try it online!](https://tio.run/##FcqxDoMgFEDR3a94m4@gxA5dap/d@AlDA7FiMASMNgwM/jq107nDXU0yx7S77duG@JmLpRJoQEmZBjkuNCfjcRRCZF7XSqzRBc01U@/wktieubkmRe35hz0y07r0lY07YDI7OCC49RdPgnvXXcU5q2CK4Yh@Fj4u6Bqw6Bjryw8 "JavaScript (Node.js) – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 92 93 bytes
```
f=lambda n,x=1:n==sum(sum(map(int,`i`))==sum(map(int,`x`))for i in range(x))and x or f(n,x+1)
```
[Try it online!](https://tio.run/##XY5BCsIwEEX3nmKomxkSxequkJ7ERaJtNGKnJbaQnj6OtAi6mFk8/n/8YR7vPR9z9ubpukvjgHUyZcXGvKYOP9e5AQOP2gZLtOAvSoJ8HyFAYIiOby0mIscNJBDsUWyqpDxEiUNx5mL/6AOjuEBBAbtangLrMZCFP1OpTweizXYpx7aZri2uK5Oeq98lapYtem1KL78B "Python 2 – Try It Online\"Online")
All I can get for now but I suspect someone much more competent than I am could do much better.
[Answer]
# Java 8, 138 bytes
```
n->{for(int a=9,c,i;;a++){for(c=i=0;++i<a;)c+=(i+"").chars().map(q->q-48).sum()==(a+"").chars().map(q->q-48).sum()?1:0;if(c==n)return a;}}
```
[Try it online.](https://tio.run/##hZFBbsIwEEX3nGKUlS2TKEFUpTWmJygbllUXU5O0QxMn2A5ShXL21AmpBGLRzVj/ezR@f3zAE8aH/XevS3QOXpHMeQZAxue2QJ3DdpCjAZoN1XAZnG4WivPoScMWDKjexJtzUduxB9XTXM9JShSCj65WpFIpBK1Rci0UIxFFPNFfaB3jSYUNO8abY7xc8cS1FeNKMfyn5SV7TiUVYbYy3Oa@tQZQdl0vB7im/SgD3MR4qmkPVUjHdt6S@Xx7B@SXaH/QpDIJtFaLNByBe7wE8LnzjMbQk1gurtXD47VaZTfqpjPLVne7G7kuTwwIE9Lux/m8SurWJ02g9aVhEbJIhKVxUBAJk4TP4NO0rv8F)
**Explanation:**
```
n->{               // Method with integer as both parameter and return-type
  for(int a=9,     //  Integer `a_i`, starting at 9 (since a(1)=10)
          c,       //  Count-integer
          i;       //  Loop-integer
      ;            //  Loop indefinitely:
       a++){       //    After every iteration: increase `a_i` by 1
    for(c=i=0;     //   Reset both the count and loop integers to 0
        ++i<a;)    //   Inner loop `i` in the range (0,a):
      c+=          //    Increase the count by:
         (i+"").chars().map(q->q-48).sum()
                   //     If the digit-sum of `i`
           ==(a+"").chars().map(q->q-48).sum()?
                   //     equals the digit-sum of `a_i`:
            1      //      Increase the count by 1
           :       //     Else:
            0;     //      Leave the count the same by increasing with 0
    if(c==n)       //   If the count equals the input integer `n` after the inner loop:
      return a;}}  //    Return `a_i` as our `a(n)` result
```
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), ~~74~~ 72 bytes
```
$a=@{}
1.."$args"|%{for(;$_-$a.(++$i|%{"$_"[0..9]-join'+'|iex})++){}}
$i
```
[Try it online!](https://tio.run/##TZHNboMwEITvPMWKOg0Igmz@3SpSrj310ltVRSTZNFSAU@I0kQjPTg0Bt77NN7OzsvYoLlifDlgUHdnDEpqOZMtV0xrM80yS1Z8n8zZr9qK2nsl6QTLPchySK2SStflOPY9/LL5EXs2d@S3Ha2s7jt20rUHyrjWMlWWAeq7FXGDUHoXvgq8FC5QVRFomSiZskqHKhrEOR8qN/2Q61PJAa5VmzE91GUsViGgygTjmLnBOdUOc0L6CpvxOHmAryhIrCdlG/CBk1Q7O1cQ2WIgLSAElogR5QHh7eYUiL3M5TqudlPaVYRT5tobRAFPq83@wT/bR2Kep@r8NN5hBM9ikOpcbrF0geD3iVuJOnYas716Np3MhFXhUFxuTg2MSazQX@K0n7adpxDTa7hc "PowerShell – Try It Online")
`2^31 = 2147483647`, so the expression `"$_"[0..9]` works with any Powershell `[int] > 0`.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 92 or 113 bytes
Brute force short version:
```
(t=Total@IntegerDigits@#&;Catch@Do[If[Count[Range@(k-1),x_/;t@x==t@k]==#,Throw@k],{k,∞}])&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7X6PENiS/JDHHwTOvJDU9tcglMz2zpNhBWc3aObEkOcPBJT/aMy3aOb80ryQ6KDEvPdVBI1vXUFOnIl7fusShwta2xCE71tZWWSckoyi/HMjWqc7WedQxrzZWU@1/QFFmXolDmoOhwX8A "Wolfram Language (Mathematica) – Try It Online")
Time- and memory-optimized version. Print table of values up to given number:
```
(n=0;a=<||>;r={};Do[While[FreeQ[a,i+1],a[k]=Lookup[a,k=Total@IntegerDigits@++n,0]+1];AppendTo[r,{i,n}],{i,#}];r)&
```
[Try it online!](https://tio.run/##FcpBC8IgGIDhHxNEoYftbIbBGAQdCgYdxMNHfbgPNxWz0@Zvt3V64eGdIY84Q6YXVCvrwctGgDyt61kkuRTRBf0caULdJ8SHBk6sNRy0M/IWgvvGjZwcQoZJXX1Gi6kjS/mjGPO8MdstLjGifw9BJ74Q98X8sytGpOO@3hP5rKxqm/oD "Wolfram Language (Mathematica) – Try It Online")
Ungolfed self-explained:
```
next = 0; assoc = <||>;
max = 30; result = {};
Do[
 While[FreeQ[assoc, n + 1],
  next += 1;
  key = Total@IntegerDigits@next;
  assoc[key] = Lookup[assoc, key, 0] + 1
  ];
 AppendTo[result, {n, next}]
 , {n, max}]; result
```
[Answer]
# [Scala](https://www.scala-lang.org/), 115 bytes
Golfed version. [Try it online!](https://tio.run/##Zc@xboMwEAbgPU9xoy0qC1J1wb2hUpdImUI7RVHkEKAuYEfnoyhCPDuFZivb6ftPuv9Cbhoz@ct3kTMcusv9w2cLwbABuBYltMY6YagKKbwRmfsxY7KuOskUPp1lwL9NgNus3DhRikTKf7JdyfNa4hW9rCmJ4weOm2lpVwqXws6xxOHHENBTjbFeph73NvBxjk5C6qsfKMJE10iK/eMD1ZqbOCsT3m1lWarQtbpPI6zH/ss2hehV7jvH4oxYy1d0UtM4zWd/AQ)
```
def f(n: Int)={var r,k=0;var w=List[Int]();do{r+=1;k=r.toString.map(_.asDigit).sum;w:+=k}while(w.count(_==k)<=n);r}
```
Ungolfed version. [Try it online!](https://tio.run/##ZY9Ni4MwFEX3/oq7TBgI2jIbqYuBbga66seqlJJatZlqUpLnSBn87TZVcTFuAue8e8l7LpWl7Dpz@clSwra@PPdm93b4C4BrlqOSSjNpCxfjy1r5PO7IKl2ceIyDVoSkTwIPb6nULGcR5//MYmaWcxPO1OdcRWE4yDYY98uZjvGtiffvtM6vtLCewokaTxvl6OhjJ8Ynf59SVzOW4asfCaIR3gkryAyXi0o@2FlIt1aFIi5cXY25BrFv3Xtq0dxUmYE1IjW1JnZG4mccqwR6@Nz2Z7RB170A)
```
object RubyToScala {
  def main(args: Array[String]): Unit = {
    println(f(1))
    println(f(2))
    println(f(3))
    println(f(30))
    println(f(50))
    println(f(1000))
  }
  def f(n: Int): Int = {
    var r = 0
    var w = List[Int]()
    var k = 0
    do {
      r += 1
      k = r.toString.map(_.asDigit).sum
      w :+= k
    } while (w.count(_ == k) <= n)
    r
  }
}
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 14 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
@¶XÇìxÃè¶Xìx}a
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=QLZYx%2bx4w%2bi2WOx4fWE&input=MTM)
[Answer]
# [Lua](https://www.lua.org/), 93 bytes
```
t={}n=0repeat m=0n=n+1(n..''):gsub('.',load'm=m+...')t[m]=(t[m]or 0)+1until...+0<t[m]print(n)
```
[Try it online!](https://tio.run/##FYkxCoAwDAC/4paWSklXMS8Rh4oiQhulppP49lhvuOEu1agq9LxMWLZri9JlQiZ2wbD3AHbY77oY8NCnM66QKTvfhpUpz2R@n6VD60JlOVJbDse/XuVgMWxVNWDjAw "Lua – Try It Online")
Ungolfed version:
```
t={}n=0
repeat 
    m=0 n=n+1                        --Increment n, set m to 0
    (n..''):gsub('.',load'm=m+...')  --Sum digits of n, store in m
    t[m]=(t[m]or 0)+1                --Increment t[m], or set to 1 if null
until...+0<t[m]                      --If input < t[m], stop loop
print(n)
```
[Answer]
# [Thunno 2](https://github.com/Thunno/Thunno2), 6 [bytes](https://github.com/Thunno/Thunno2/blob/main/docs/codepage.md)
```
ƘRʂṪc=
```
[Try it online!](https://Not-Thonnu.github.io/run#aGVhZGVyPSZjb2RlPSVDNiU5OFIlQ0ElODIlRTElQjklQUFjJTNEJmZvb3Rlcj0maW5wdXQ9MTMmZmxhZ3M9)
#### Explanation
```
ƘRʂṪc=  # Implicit input
Ƙ       # Find the first positive integer, n, where:
 R      #  Push [1..n]
  ʂ     #  Get the digital sum of each number
   Ṫ    #  Remove the last item and push it separately
    c   #  Count the occurrences of this in the list
     =  #  Does this equal the input?
```
]