Create a program that takes input of a positive non-zero integer and outputs the 4 next numbers in the sequence described below.

Note: Checking if the input is actually a positive non-zero integer is not necessary


Every number in this sequence (apart from the first, which is the input) shall be composed of n digits, where n is an even number. If we split the number to n/2 pairs, for each pair, the first digit should be the amount of times the second digit appeared in the previous number

Visual explanation:
Consider this example "sequence starter" or input 6577
The next number in the sequence should look like this 161527
Because the input has 1 "6", 1 "5" and 2 "7"s.

If input has too many digits (more than 9 of a single digit) you wouldnt be able to get a correct output
Example: 111111111111 (12 1's)
Next number in sequence has to describe 12 1's. Thus we split it into 9 1's and 3 1's (sum 9+3 = 12)
Next number: 9131

You should iterate 4 times for the input, and output it (either return a list/array of 4 integers, or output it by seperating them with a space, newlines are also acceptable)

"The number can be written in a lot of ways, how do I write it?":
If you think about it, the example input 6577 can also be written as 271516 (two 7's, one 5, one six). However this is non-valid output. You should iterate the number left to right. Thus 161527. If it was 7657 you would iterate the amount of 7's, then amount of 6's then amount of 5's, thus valid output would be 271615

Example I/O:

Output:1715 211715 12311715 4112131715

Output:11 21 1211 3112

Input:111111111111 (12 1's)
Output:9131 192113 31191213 23411912

This is unlike the "Say what you see" question, because the sequences are different: https://oeis.org/A005150 <- This one returns numbers like this:
Input: 1211 Output: 111221
While the sequence I'm asking for would do
Input: 1211 Output: 3112

The two sequences are different and require different algorithms.
My asked sequence: https://oeis.org/A063850
"Possible duplicate" sequence: https://oeis.org/A005150

Important specification:

Since it wasnt clear enough for some people who tried to answer this question, the correct output for k chars where k > 9 is not "kc" (where c is char) but 9c(k-9)c etc. Thus correct output for 12 1's isn't 121 (12 1) but 9131(9 1's, (12-9) 1's and so on)

If in doubt, your code is wrong if it ever outputs a number with an odd amount of digits (like 121), it should have output of even digit numbers due to the nature of the sequence.

This is thus code with least bytes wins.

  • \$\begingroup\$ Proposed testcase: 1111111111111111111 (19 1's) \$\endgroup\$
    – Emigna
    Commented Feb 1, 2017 at 14:55
  • \$\begingroup\$ More closely related (still not dupe though). \$\endgroup\$ Commented Feb 1, 2017 at 17:42
  • \$\begingroup\$ Can we output as a comma-separated list of integers? Can the output start with the input integer (and thus have length 5)? \$\endgroup\$ Commented Feb 1, 2017 at 17:50
  • \$\begingroup\$ In your last test case, shouldn't the last number be 23411912 instead of 23411219? \$\endgroup\$ Commented Feb 1, 2017 at 17:51
  • \$\begingroup\$ @GregMartin Indeed. Thanks for pointing out. But no, you are not allowed to return a list of integers or output integers seperated with anything apart from newlines or spaces. And no, you shouldnt output the input \$\endgroup\$
    – Offtkp
    Commented Feb 1, 2017 at 18:01

7 Answers 7


PowerShell, 111 104 bytes


Try it online!

  • \$\begingroup\$ Since you don't use $i in your loop, why not just loop directly like $z=$args;0..3|%{... \$\endgroup\$ Commented Feb 1, 2017 at 14:27
  • \$\begingroup\$ @AdmBorkBork I thought about it but felt it would be longer after having to assign $args (and I originally thought I would use $i). I was going to measure it but then the question got closed. \$\endgroup\$
    – briantist
    Commented Feb 1, 2017 at 14:30
  • \$\begingroup\$ @AdmBorkBork ...aaaand edited (thanks) \$\endgroup\$
    – briantist
    Commented Feb 1, 2017 at 14:32

Python 2, 116 bytes

exec"x=''.join(x.count(n)/9*(`9`+n)+`x.count(n)%9`+n for i,n in enumerate(x)if n not in x[:i]);print x;"*4

Try it online!

  • \$\begingroup\$ Output for 19 ones 1111111111111111111 is incorrect. Should be 919111 but gives 919121 \$\endgroup\$
    – CSharpie
    Commented Feb 1, 2017 at 21:51
  • \$\begingroup\$ Output wrong for many values. Example: image.prntscr.com/image/ed4c523b105b41169e8aa8c46a95f963.png , With input 11 output should be 21 1211 3112 132112, I dont understand why it outputs 111 as the first iteration which causes the whole chain to go badly \$\endgroup\$
    – Offtkp
    Commented Feb 1, 2017 at 21:56
  • \$\begingroup\$ @P.Ktinos the input format was wrong, it need to be a string (it must be the leftover of the tests that i was doing). Fixed the link \$\endgroup\$
    – Rod
    Commented Feb 1, 2017 at 23:15

05AB1E, 30 23 21 bytes


Try it online!


4F                     # 4 times do:
  ©                    # store a copy of the current number in register
   Ùv                  # for each unique digit y in the number
     9y«               # concatenate 9 with y
        ®y¢            # count occurrences of y in current number
           9‰          # divmod by 9
             `U        # store the result of modulus in X
               ×       # repeat the number "9y" result_of_div times
                X      # push result of modulus
                 y     # push y
                  J    # join everything to one number
                   }   # end inner loop
                    =  # print the current number without popping
  • \$\begingroup\$ @MagicOctopusUrn: That won't work for number with more than 9 repetitions of a digit, like the example in my TIO link for example. \$\endgroup\$
    – Emigna
    Commented Jul 21, 2017 at 8:15
  • \$\begingroup\$ Ohhh... I got it now. \$\endgroup\$ Commented Jul 22, 2017 at 4:12

Mathematica, 117 bytes


Seems like it shouldn't need to be this long.


C# 246 bytes

namespace System{using Linq;using f=String;class p{static void Main(f[] s){f p=s[0];for(int i=0,n;i++<4;Console.Write(p+" "))p=f.Concat(p.GroupBy(c=>c).SelectMany(g=>new int[(n=g.Count())/9].Select(_ =>"9"+g.Key).Concat(new[]{n%9+""+g.Key})));}}}


namespace System
    using Linq;
    using f = String;
    class p
        static void Main(f[] s)
            f p = s[0];
            for (int i = 0, n; i++ < 4; Console.Write(p + " "))

                p = f.Concat(p.GroupBy(c => c).SelectMany(g =>
                    new int[(n = g.Count()) / 9].Select(_ => "9" + g.Key).Concat(new[] { n % 9 + "" + g.Key }

Try it here (Type input into bottom frame once its compiled and hit ENTER)


Jelly, 27 bytes


Try it online!

The successive s cannot be nested because chains cannot be nested.

Nesting with separate link: 27 bytes.

Print instead of cumulation: 27 bytes.


L,Ḣ                     - helper function, does the look-and-say. Input is a list of digits
 ,                      - return 2-tuple of:
L                       -   length of input
  Ḣ                     -   first element of input

D©®i$ÞŒgs9$€Ç€€FḌµ4µÐ¡Ḋ - main link, takes input as integer
                 µ4µÐ¡  - repeat 4 times, saving the results of each iteration:
D                       -   convert integer to list of digits
 ©                      -   save into register, for later use
  ®i$Þ                  -   sort list's elements by first occurrence in list
      Œg                -   group runs of equal elements
        s9$€            -   split each run into sets which are at most 9 elements long
            Ç€€         -   do the look-and-say with helper function
               FḌ       -   flatten and convert back into integer for next iteration
                      Ḋ - remove the first element from the list since it includes the
                           initial element

PHP, 141 Bytes


Try it online!


Your Answer

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

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