19
\$\begingroup\$

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       | [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 !

\$\endgroup\$
6
  • 4
    \$\begingroup\$ 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}\$. \$\endgroup\$
    – Bubbler
    Feb 6, 2020 at 1:51
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Feb 6, 2020 at 11:05
  • 1
    \$\begingroup\$ @EricDuminil Yes I invented it and I'm actually half-tempted to submit it on OEIS. \$\endgroup\$
    – Arnauld
    Feb 6, 2020 at 11:08
  • 6
    \$\begingroup\$ @EricDuminil OEIS draft \$\endgroup\$
    – Arnauld
    Feb 6, 2020 at 12:16
  • 2
    \$\begingroup\$ It's all about the base. \$\endgroup\$
    – S.S. Anne
    Feb 6, 2020 at 18:55

17 Answers 17

10
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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)
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 10 bytes

∞.Δ1Ÿ1öć¢Q

Try it online!

\$\endgroup\$
1
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 16 bytes

W⁻№υΣLυθ⊞υΣLυILυ

Try it online! Link is to verbose version of code. Explanation:

W⁻№υΣLυθ

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

⊞υΣLυ

... append the digital sum of the length of the list to the list.

ILυ

Output the length of the list.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 55 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
0
1
\$\begingroup\$

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.

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

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Wolfram Language (Mathematica), 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!

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!

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
\$\endgroup\$
0
\$\begingroup\$

Scala, 115 bytes

Golfed version. Try it online!

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!

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

\$\endgroup\$
0
\$\begingroup\$

Japt, 14 bytes

@¶XÇìxÃè¶Xìx}a

Try it

\$\endgroup\$
0
\$\begingroup\$

Lua, 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!

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)
\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 6 bytes

ƘRʂṪc=

Try it online!

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?
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.