# It's all about the sum of the digits

## 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

## First terms

  n | a(n) | sum of digits | matching integers
----+------+---------------+------------------------------------------------------
1 |  10  |       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 !

• A simple proof that $a_n$ is defined for any $n$: $10^n$ has digit sum of 1, and has exactly $n$ positive integers smaller than itself whose sum of digits is 1, namely $10^0 \cdots 10^{n-1}$. – Bubbler Feb 6 at 1:51
• Did you invent a(n)? I couldn't find it in oeis.org. That's an interesting function in order to troll "Continue the sequence" questions. What comes after 10, 20, 30, 40, 50, 60, 70, 80, 90? 108. – Eric Duminil Feb 6 at 11:05
• @EricDuminil Yes I invented it and I'm actually half-tempted to submit it on OEIS. – Arnauld Feb 6 at 11:08
• @EricDuminil OEIS draft – Arnauld Feb 6 at 12:16
• It's all about the base. – S.S. Anne Feb 6 at 18:55

# Pyth, 12 bytes

f}hQ_XsjT;H1


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.

# Gaia, 13 10 bytes

1⟨┅Σ¦ṅC=⟩#


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


# Jelly, 10 bytes

D€§ċṪ$=ʋ1#  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)


# 05AB1E, 10 bytes

∞.Δ1Ÿ1öć¢Q


Try it online!

# APL (Dyalog Unicode), 27 bytesSBCS

{⌈⌿+\∘.=⍨1⊥10⊥⍣¯1⍳20×⍵}⍳+∘1


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


# Python 2, 71 bytes

f=lambda n,x=:x.count(x[-1])+~n and-~f(n,x+[sum(map(int,len(x)))])


Try it online!

# Charcoal, 16 bytes

Ｗ⁻№υΣＬυθ⊞υΣＬυＩＬυ


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.

# JavaScript (Node.js), 6867 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!

# Python 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!

All I can get for now but I suspect someone much more competent than I am could do much better.

• Because you define the function recursively, you have to give it a name, so you have 94 bytes :/ at least for now :) – RGS Feb 6 at 0:34
• And then 93 bytes if you put the n== in the beginning, which allows for the next keyword to come closer to the ). – RGS Feb 6 at 0:53
• I see and thanks @RGS – mabel Feb 6 at 8:50

# Ruby, 55 bytes

->n,r=0,*w{w<<k=(r+=1).digits.sum until w.count(k)>n;r}


Try it online!

# 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.

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


# PowerShell, 74 72 bytes

$a=@{} 1.."$args"|%{for(;$_-$a.(++$i|%{"$_"[0..9]-join'+'|iex})++){}}
$i  Try it online! 2^31 = 2147483647, so the expression "$_"[0..9] works with any Powershell [int] > 0.