Let's define the a function on natural numbers \$n\$, written as base 10 digits \$d_k\; d_{k-1}\; \dotsc\; d_1\; d_0\$, as follows:

As long as there are equal adjacent digits \$d_i\;d_{i-1}\$, replace them by their sum \$d_i+d_{i-1}\$ from left to right. If there were any such digits, repeat the same procedure.

In other words, in each iteration we greedily take all pairs of equal adjacent digits and replace them by their sum at the same time (using the left-most pair if they overlap).


Let's take \$\texttt{9988}\$ for example:

  1. The first adjacent digits which are equal are the two \$\texttt{9}\$
  2. So we replace them by \$\texttt{9 + 9} = \texttt{18}\$ which gives us \$\texttt{1888}\$
  3. Since we're still in the first left-right traversal and there were still two \$\texttt{8}\$s we need to first replace these
  4. So we get \$\texttt{1816}\$
  5. Something changed, so we need to do another iteration
  6. But there are no such digits, so we stop

Therefore the \$9988^\text{th}\$ number in that sequence is \$1816\$.


The first 200 terms are:


Your task is to generate that sequence, either

  • given \$n\$, return the \$n^\text{th}\$ number in that sequence,
  • given \$n\$, return the first \$n\$ numbers in that sequence
  • or generate the sequence indefinitely.

You may choose your submission to use either \$0\$- or \$1\$-indexing, but please specify which.

Test cases

You may use the above given terms, however here are some larger ones:

222 -> 42
1633 -> 4
4488 -> 816
15519 -> 2019
19988 -> 2816
99999 -> 18189
119988 -> 21816
100001 -> 101
999999 -> 181818

18 Answers 18


Python 3, 128 bytes

def t(z):j=z and(z[0]==z[1:2])+1;return[str(int(z[0])*j),*t(z[j:])]if j else''
def c(n):r="".join(t(n));return r!=n and c(r)or r

Try it online!

  • 5
    \$\begingroup\$ Welcome to PPCG! Nice first post! \$\endgroup\$ – Rɪᴋᴇʀ Jan 2 '19 at 18:33
  • 1
    \$\begingroup\$ 108 bytes \$\endgroup\$ – Jo King Jan 3 '19 at 2:09

Python 2, 97 96 93 bytes

def f(n):r=re.sub(r'(.)\1',lambda m:`int(m.group(1))*2`,n);return r!=n and f(r)or r
import re

Try it online!

Non regex version:

Python 2, 133 130 122 112 98 bytes

def f(n):
 while s:a=1+(s[0]==s[1:2]);r+=`int(s[0])*a`;s=s[a:]
 return r!=n and f(r)or r

Try it online!


Jelly, 11 bytes


This is an unnecessarily slow, full program.

Try it online!

Alternate version, 12 bytes


One byte longer, but much faster. Works as a program or a function.

Try it online!

How it works

DŒg+2/€FVµƬṪ  Main link. Argument: n (integer)

         µ    Combine the previous links into a chain. Begin a new one.
D               Decimal; yield n's digit array in base 10.
 Œg             Group adjacent, identical digits into subarrays.
   +2/€         Map non-overlapping, pairwise sum over the subarrays.
                If there is an odd number of digits in a subarray, the
                last digit will remain untouched.
       F        Flatten; dump all sums and digits into a single array.
        V       Eval; turn the result into an integer.
          Ƭ   Execute the chain 'til the results are no longer unique.
              Return all unique results.
           Ṫ  Tail; extract the last result.

The 11-byte version does the same, except it calls the link n times for input n, instead of calling it until a fixed point is reached.

  • 3
    \$\begingroup\$ It's not unnecessary if it saves 1 byte :-) \$\endgroup\$ – Luis Mendo Jan 3 '19 at 1:29

Haskell, 70 bytes

f(a:b:c)|a==b=show(2*read[a])++f c|1<2=a:f(b:c)
f a=a

Input is taken as a string.

Try it online!

  • \$\begingroup\$ It doesn't save you anything so far, but with the same length you can replace the second clause with |x<-b:c=a:f x or even f(a:c)=a:f c, in case one or the other could actually lead to an improvement:) \$\endgroup\$ – flawr Jan 2 '19 at 20:57

JavaScript, 48 47 46 bytes

Input and output as strings. Returns the nth term of the sequence.


Try it online

  • 1 byte saved thanks to Arnauld
  • 1 byte saved thanks to tsh
  • 1
    \$\begingroup\$ x[0]*2 -> x/5.5 \$\endgroup\$ – tsh Jan 3 '19 at 7:22
  • \$\begingroup\$ Thanks, @tsh. Wouldn't have thought of that. \$\endgroup\$ – Shaggy Jan 3 '19 at 23:01

Perl 6, 37 bytes


Try it online!

This is a function which generates the nth term of the sequence, given n as its argument.

($_, { ... } ... * == *) is the sequence of successive changes to the input number, generated by the bracketed expression (a simple regex substitution) and stopping when * == *, that is, when the last two numbers in the sequence are equal. Then the [*-1] takes just the final element of that sequence as the return value.

  • \$\begingroup\$ You can save bytes by removing the ==* and replacing the *-1 with $_, since there are always less than n replacements for a number n. 33 bytes \$\endgroup\$ – Jo King Jan 3 '19 at 1:51

Retina, 16 bytes


Try it online! Link includes test cases. Explanation:


Repeat until the input stops changing.


Replace pairs of adjacent digits...


... with twice the digit. ($1* generates a string of $1 _s, 2* duplicates that, and $.( takes the length. Actually, the Retina engine is cleverer than that and just doubles $1.)


C# (.NET Core), 231, 203, 200, 196, 192 bytes

EDIT: Function is now at 185 bytes plus 18 for using System.Linq;

Thanks to BMO (for 1>0 being equal to true plus newline removal) and Mr. XCoder (for f=!f statements)!

EDIT2: Down to 182 bytes plus 18 for using System.Linq thanks to dana for sharing a few golf tips!

EDIT3: Thanks to Embodiment of Ignorance for the int[] -> var, removal of short circuit && -> &, and changing up ToArray -> ToList! (178 bytes + 18 using)

EDIT4: Embodiment of Ignorance dropped 4 bytes by changing an assignment. Dummy me shoulda counted! Thanks again :D

p=>{var f=1>0;while(f){var t=p.Select(n=>n-48).ToList();p="";f=!f;for(var j=0;j<t.Count;j++){if(j<t.Count-1&t[j]==t[1+j]){p+=t[j]+t[++j];f=!f;continue;}p+=t[j];}};return p;};

Try it online!


Perl 5 -p, 21 bytes


Try it online!


Japt v2.0a0 -h, 15 14 bytes

Returns the nth term of the sequence.


Try it

This should work for 10 bytes but there seems to be a bug in Japt's recursive replacement method.


Groovy, 63 bytes

while(s=~r)s=s.replaceAll(r){(it[1]as int)*2}

Try it online!


05AB1E, 11 bytes


Try it online or verify all test cases.


Δ             # Continue until the (implicit) input no longer changes:
 γ            #  Split the integer in chunks of the same adjacent digits
              #   i.e. 199999889 → [1,99999,88,9]
  ε     }     #  Map each to:
   2ô         #   Split it into parts of size 2
              #    i.e. 99999 → [99,99,9]
     €S       #   Split each part into digits
              #    i.e. [99,99,9] → [[9,9],[9,9],[9]]
       O      #   And take the sum of each part
              #    i.e. [[9,9],[9,9],[9]] → [18,18,9]
         ˜    #  Flatten the list
              #   i.e. [[1],[18,18,9],[16],[9]] → [1,18,18,9,16,9]
          J   #  Join everything together
              #   i.e. [1,18,18,9,16,9] → 118189169
              # (And output the result implicitly at the end)
              #  i.e. output = 28189169

Wolfram Language 108 bytes



IntegerDigits transforms the input number into a list of its digits.

Split groups consecutive repeated digits.

Partition[#, UpTo@2]&/@ breaks runs of like digits into lists of, at most, lengths of 2.

Flatten[...,1] eliminates occasional overly-nested braces--e.g., {{2,2}} becomes {2,2}

Total/@ sums totals of paired digits. Isolated digits need not be summed.

ToString converts the totals (and isolated digits) to strings.

""<> joins all the strings in the list.

ToExpression converts the outcome to an integer.

...~FixedPoint~#& applies the function until the result ceases to change.


C# (Visual C# Interactive Compiler) with flag /u:System.Text.RegularExpressions.Regex, 70 bytes


Outputs by modifying the input. Takes in a list containing one string for input.

Thanks to @dana for golfing an entire 23 bytes!

Try it online!

  • \$\begingroup\$ 95+34 - 33 + 1 for the extra space you need in the commandline args, iirc \$\endgroup\$ – ASCII-only Jan 3 '19 at 6:23
  • \$\begingroup\$ Recursive anonymous functions have to be defined first, and the definition is in included in the byte count. \$\endgroup\$ – Embodiment of Ignorance Jan 3 '19 at 6:42
  • \$\begingroup\$ Oh, it's recursive \$\endgroup\$ – ASCII-only Jan 3 '19 at 7:18
  • 1
    \$\begingroup\$ Nice! I think I can get it down a bit more \$\endgroup\$ – Embodiment of Ignorance Jan 27 '19 at 16:30
  • \$\begingroup\$ That's a pretty good score considering it's C# :) \$\endgroup\$ – dana Jan 28 '19 at 5:44

Clean, 118 bytes

import StdEnv,Data.List
$[a,b:t]|a==b=[1,(a*2)rem 10]%(1-a/5,1)++ $t=[a: $[b:t]]

limit o iterate$o map digitToInt

Try it online!

Takes the first repeated value (limit) from the infinite list of applications (iterate) of a lambda performing a single step of the collapsing process. Input taken as a [Char].


Red, 84 83 80 bytes

func[n][if parse s: form n[to some change[copy d skip d](2 * do d)to end][f s]s]

Try it online!

Returns the nth term of the sequence.


f: func [ n ] [
    if parse s: form n [  ; parse the input converted to a string
        to some change [  ; find and change one or more
            copy d skip   ; digit (in fact any character, no predefined character classes)
            d             ; followed by itself
        ] (2 * do d)      ; with its doubled numeric value 
        to end            ; go to the end of the string
    ] [ f s ]             ; call the function with the altered string if parse returned true
    s                     ; finally return the string 

Scala, 84 bytes

s=>{var t=s

Try it online!


C# (Visual C# Interactive Compiler), 111 bytes

s=>{var t=s;do{s=t;t="";for(int i=0;i<s.Length;)t+=s[i]%48*(s[i++]!=(s+0)[i]?1:2*++i/i);}while(t!=s);return t;}

Try it online!

HUGE credit to @ASCIIOnly for golfing ~30 ;) At first we were both posting updates simultaneously, but at some point he clearly went to town!

-2 thanks to @EmbodimentOfIgnorance!

Less golfed code...

// s is the input as a string
  // t is another string used
  // to hold intermediate results
  var t=s;
  // the algorithm repeatedly
  // processes s and saves the
  // result to t
    // copy the last result to s
    // and blank out t
    // iterate over s
    for(int i=0;i<s.Length;)
      // append either 1 or 2 times
      // the current digit to t
        // compare the current digit
        // to the next digit. to prevent
        // an out-of-bounds exception,
        // append a 0 to s which either
        // gets ignored or collapses
        // to 0
          // if they are different, then
          // the multiplier is 1
          // if they are the same, then
          // the multiplier is 2, and we
          // have to increment i
  // continue this until the input
  // and output are the same
  return t;
  • \$\begingroup\$ 139 \$\endgroup\$ – ASCII-only Jan 3 '19 at 6:47
  • \$\begingroup\$ 134 \$\endgroup\$ – ASCII-only Jan 3 '19 at 6:52
  • \$\begingroup\$ @ASCIIOnly - Good move :) (s[i++]-48)*2 => s[i++]*2-96 \$\endgroup\$ – dana Jan 3 '19 at 6:54
  • \$\begingroup\$ 131 \$\endgroup\$ – ASCII-only Jan 3 '19 at 6:59
  • 1
    \$\begingroup\$ 114 \$\endgroup\$ – ASCII-only Jan 3 '19 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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