19
\$\begingroup\$

Challenge:

Here we have the first 100 items of a sequence:

6,5,4,3,2,1,66,65,64,63,62,61,56,55,54,53,52,51,46,45,44,43,42,41,36,35,34,33,32,31,26,25,24,23,22,21,16,15,14,13,12,11,666,665,664,663,662,661,656,655,654,653,652,651,646,645,644,643,642,641,636,635,634,633,632,631,626,625,624,623,622,621,616,615,614,613,612,611,566,565,564,563,562,561,556,555,554,553,552,551,546,545,544,543,542,541,536,535,534,533,...

How is this sequence formed? We first have the number in the range [6, 1] (all possible values of a single die from highest to lowest). We then have the numbers [66..61, 56..51, 46..41, 36..31, 26..21, 16..11] (all possible concatted values of two dice from highest to lowest). Etc.
This is related to the OEIS sequence A057436: Contains digits 1 through 6 only, but with all the numbers with equal amount of digits sorted backwards in the sequence.

The challenge is to choose one of these three options for your function/program with the sequence above:

  1. Take an input \$n\$ and output the \$n\$'th value of this sequence, where it can be either 0-indexed or 1-indexed.
  2. Take an input \$n\$ and output the first \$n\$ or \$n+1\$ values of this sequence.
  3. Output the values from the sequence indefinitely.

Of course, any reasonable output format can be used. Could be as strings/integers/decimals/etc.; could be as an (infinite) list/array/stream/etc.; could be output with space/comma/newline/other delimiter to STDOUT; etc. etc. Please state what I/O and option you're using in your answer!

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code (i.e. TIO).
  • Also, adding an explanation for your answer is highly recommended.

Here some larger test cases if you choose option 1:

n         0-indexed output    1-indexed output

500       5624                5625
750       4526                4531
1000      3432                3433
9329      11111               11112
9330      666666              11111
9331      666665              666666
10000     663632              663633
100000    6131232             6131233
\$\endgroup\$
  • 1
    \$\begingroup\$ Per the suggested edit, kolmogorov complexity tag does not apply to sequences, only to a constant, finite, fixed output. A sequence goes on forever. \$\endgroup\$ – mbomb007 Feb 23 at 21:46
  • \$\begingroup\$ In addition to what @mbomb007 said, I also allow outputting the n'th value or first n/n+1 values based on an input, whereas KC challenges will never have inputs. \$\endgroup\$ – Kevin Cruijssen Feb 23 at 22:23

25 Answers 25

2
\$\begingroup\$

Jelly, 5 bytes

ḃ6ạ7V

Try it online!

1-indexed nth value.

\$\endgroup\$
12
\$\begingroup\$

Perl 6, 24 23 bytes

-1 byte thanks to nwellnhof

{.put;.[]X~(6...1)}...*

Try it online!

Outputs the sequence infinitely separated by spaces/newlines. Or, for a few more bytes we can have a lazy infinite list we can index into instead.

Perl 6, 27 bytes

{flat {@=.[]X~(6...1)}...*}

Try it online!

Explanation:

{                         }    # Anonymous code block
 flat                          # Return the flattened
                      ...*       # Infinite sequence
      {              }             # Defined as
         .[]                       # The previous element arrayified
            X~                     # Each concatenated with
              (6...1)              # All of 6 to 1
       @=                          # Arrayified
\$\endgroup\$
6
\$\begingroup\$

Python 2, 39 38 34 bytes

f=lambda n:n and-n%6+1+f(~-n/6)*10

Try it online!

Outputs 1-indexed number

\$\endgroup\$
6
\$\begingroup\$

R, 43 bytes

p='';repeat cat(p<-sapply(p,paste0,6:1),'')

Try it online!

Prints the sequence indefinitely

  • -9 thanks to @Kirill L.
\$\endgroup\$
  • 1
    \$\begingroup\$ @tk3: without the second parameter it will concatenate the last value of the sub-sequence of n-digits elements, with the first value of the sub-sequence of n+1 digits elements. e.g. 6 5 4 3 2 166 65 64... \$\endgroup\$ – digEmAll Feb 15 at 18:39
6
\$\begingroup\$

Bash, 31 bytes

f()(x+={6..1};eval echo $x;f);f

TIO

update from comments, the n'th value 1-indexed, +GNU tools + perl, 64 bytes, 7 bytes saved thanks to @manatwork

dc<<<6o$1p|perl -pe 's/(.)0/($1-1).6/e?redo:s/0//'|tr 1-6 654321

64 bytes

\$\endgroup\$
  • \$\begingroup\$ Doesn't help much, but the in the 2nd solution is shorter to escape the semicolon than double quoting the entire expression: bc<<<obase=6\;$1. But if you switch to dc, there is nothing to escape: dc<<<6o$1p. \$\endgroup\$ – manatwork Feb 15 at 14:26
  • \$\begingroup\$ thanks indeed it saves 7bytes but because of bijective numeration it still not working a mix bash perl (66bytes) dc<<<6o$1p|perl -pe '1while s/(.)0/($1-1).6/e;s/0//'|tr 1-6 654321 \$\endgroup\$ – Nahuel Fouilleul Feb 15 at 14:48
5
\$\begingroup\$

MATL, 11 bytes

`6:P!V@Z^DT

Outputs the values indefinitely.

Try it online!

Explanation

`      % Do...while
  6:   %   Push [1 2 3 4 5 6]
  P    %   Flip: gives [6 5 4 3 2 1]
  !    %   Transpose: turns the row vector into a column vector
  V    %   Convert the number in each row to the corresponding char
  @    %   Push current iteration index, starting from 1
  Z^   %   Cartesian power. Gives a matrix where each row is a Cartesian tuple
  D    %   Display immediately
  T    %   Push true. This is used as loop condition, to give an infinite loop
       % End (implicit)
\$\endgroup\$
5
\$\begingroup\$

Haskell, 38 34 bytes

An infinite list of numbers:

d=[6,5..1]
l=d++[10*m+n|m<-l,n<-d]

Try it online!

Two earlier solutions that give infinite lists of strings, each using 38 bytes:

[1..]>>=sequence.(`replicate`"654321")

Try it online!

do n<-[1..];mapM id$[1..n]>>["654321"]

Try it online!

\$\endgroup\$
5
\$\begingroup\$

JavaScript (ES6), 26 bytes

Returns the \$n\$th term, 1-indexed.

f=n=>n--&&[f(n/6|0)]+6-n%6

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Haskell, 28 bytes

l=(+).(10*)<$>0:l<*>[6,5..1]

Try it online!

Produces an infinite list of numbers l. Using <$> and <*> cuts a byte off:

29 bytes

l=[10*n+d|n<-0:l,d<-[6,5..1]]

Try it online!

The approach is similar to the Haskell Output All String answer fixed input string "654321", and skipping the empty string output by changing where it's prepended.

30 bytes

l=[n++[d]|n<-"":l,d<-"654321"]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ That's great! I saw it's shorter to start off from 0 (or ""), but didn't find a cheap way to not have it in the result... \$\endgroup\$ – Christian Sievers Feb 16 at 1:33
4
\$\begingroup\$

05AB1E, 10 bytes

Outputs the sequence indefinitely.

¸[6LRâJD»,

Try it online!

Explanation

¸           # initialize the stack with a list containing the empty string
 [          # loop
  6L        # push [1 ... 6]
    R       # reverse
     â      # cartesian product
      J     # join each inner list
       D    # duplicate (saving a copy for next iteration)
        »,  # join on newline and print
\$\endgroup\$
  • 1
    \$\begingroup\$ Never knew ¸ at the start creates a list containing an empty string. And 2 bytes shorter than the solution I used to generate the test cases, so of course a +1 from me. :) \$\endgroup\$ – Kevin Cruijssen Feb 15 at 11:50
4
\$\begingroup\$

Perl 5, 40 37 bytes

-3bytes thanks to @Xcali

map{say}<"$_">while s//{6,5,4,3,2,1}/

37 bytes

40 bytes

\$\endgroup\$
  • \$\begingroup\$ 37 bytes \$\endgroup\$ – Xcali Feb 15 at 17:41
  • \$\begingroup\$ Brilliant use of glob. +1 \$\endgroup\$ – msh210 Feb 23 at 23:26
3
\$\begingroup\$

Java (JDK), 48 bytes

String f(int n){return n-->0?f(n/6)+(6-n%6):"";}

Try it online!

This returns the 1-indexed nth element.

Recursion seems to beat the iterative lambda.


Iterative version, 49 bytes

n->{var r="";for(;n-->0;n/=6)r=6-n%6+r;return r;}

Try it online!

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

Brachylog, 13 11 bytes

Thanks to Fatalize for 2 bytes

6~d{⟧₁∋}ᵐẉ⊥

Outputs indefinitely. Try it online!

Here's a 14-byte version that outputs the first \$n\$ values: Try it online!

Explanation

6~d           Start with a number, all of whose digits are 6's
              Brachylog considers these in the order 6, 66, 666, 6666...
   {   }ᵐ     Map this predicate to each of those digits:
    ⟧₁         1-based reverse range: [6,5,4,3,2,1]
      ∋        The output digit must be a number in that range
              Brachylog considers possible outputs in this order: 6, 5, 4, 3, 2, 1, 66, 65...
         ẉ    Write a possible output with newline
          ⊥   Force the predicate to fail and backtrack to the next possibility
\$\endgroup\$
  • \$\begingroup\$ You are on a Brachylog roll! \$\endgroup\$ – Fatalize Feb 18 at 12:31
  • 1
    \$\begingroup\$ You can save 2 bytes by using a failure-driven loop, as they are called in Prolog: 6~d{⟧₁∋}ᵐẉ⊥. You basically end your program with "false" which will force it to print all solutions. \$\endgroup\$ – Fatalize Feb 18 at 12:36
  • \$\begingroup\$ Ooh, nice. Yes, I've been enjoying it a lot! \$\endgroup\$ – DLosc Feb 18 at 19:13
3
\$\begingroup\$

C# (.NET Core), 38 bytes

int f(int n)=>n-->0?f(n/6)*10+6-n%6:0;

Try it online!

Outputs the n-th value (1-based).

\$\endgroup\$
  • \$\begingroup\$ Which one is this solving? \$\endgroup\$ – Stackstuck Feb 20 at 10:03
  • \$\begingroup\$ @Stackstuck - I've now specified this in my answer. \$\endgroup\$ – dana Feb 20 at 11:13
  • 1
    \$\begingroup\$ Oh good! We're not overlapping. I just wrote the infinite printer. \$\endgroup\$ – Stackstuck Feb 20 at 11:15
2
\$\begingroup\$

Japt, 14 bytes

There's gotta be a shorter solution using function methods and/or Cartesian product but (for now?) the best I can manage is a port of Arnauld's JS solution so be sure to upvote him too.

©ß´Uz6)s+6-Uu6

Try it or test terms 0-1000

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

Wolfram Language (Mathematica), 88 78 bytes

(l=d=c=7-Range@6;While[Length@c<#,d=Flatten[(10#+l)&/@d];c=c~Join~d;];c[[#]])&

Try it online!

saved 4 + 6 bytes thanks to @IanMiller

List is 1 indexed, outputs the n'th number.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can replace Range[6,1,-1] with 7-Range@6 to save 4 characters \$\endgroup\$ – Ian Miller Feb 17 at 2:36
  • 1
    \$\begingroup\$ For codegolf rules you can also write it as an anonymous function: (l=d=c=7-Range@6;While[Length@c<#,d=Flatten[(10#+l)&/@d];c=c~Join~d;];c[[#]])& \$\endgroup\$ – Ian Miller Feb 17 at 2:39
  • \$\begingroup\$ @IanMiller thanks! I wasn't sure what the rules were about the format. \$\endgroup\$ – Kai Feb 17 at 3:04
2
\$\begingroup\$

Mathematica, 56 bytes

Flatten[FromDigits/@Tuples[7-Range@6,#]&/@Range@#][[#]]&

Requires excessive amounts of memory to actually run as it creates all numbers in the sequence which have length equal to or less than the input (i.e. it creates \$\frac{6}{5}(6^n-1)\$ terms) then takes the required value from the list. For practice use replace the second last # with a specific value for the length you want to use it on.

\$\endgroup\$
  • \$\begingroup\$ +1, that is monstrous overkill but works perfectly for brevity! \$\endgroup\$ – Kai Feb 18 at 5:24
  • \$\begingroup\$ @JonathanFrech Thanks for fixing my mathjax. I wasn't sure how to activate it here as its slightly different that math.se \$\endgroup\$ – Ian Miller Feb 26 at 7:48
  • \$\begingroup\$ Please note that the original edit was by this user. \$\endgroup\$ – Jonathan Frech Feb 26 at 20:13
  • \$\begingroup\$ Oops my bad. Thanks to @geza-kerecsenyi too. \$\endgroup\$ – Ian Miller Feb 27 at 9:53
1
\$\begingroup\$

Pip -l, 16 bytes

x:YP6-,6W1PxCP:y

Outputs the sequence indefinitely. Try it online!

Explanation

The -l flag means that lists are printed with each item on its own line; if an item is itself a list, its elements are concatenated with no separator. E.g. the list [1 [2 3] [4 [5 6]]] would be printed as

1
23
456

With that cleared up:

x:YP6-,6W1PxCP:y
      ,6          Range(6): [0 1 2 3 4 5]
    6-            Subtract each element from 6: [6 5 4 3 2 1]
  YP              Yank that value into the y variable, and also print it
x:                Assign that value also to x
        W1        While 1 (infinite loop):
           xCP:    Assign to x the cartesian product of x with
               y   the list [6 5 4 3 2 1]
          P        Print it

After the first loop iteration, x looks like [[6;6];[6;5];[6;4];...;[1;1]]; after the second iteration, [[[6;6];6];[[6;6];5];[[6;6];4];...;[[1;1];1]]; and so on. We don't need to worry about flattening the sublists, because -l effectively does it for us.

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

Charcoal, 18 bytes

NθWθ«←I⊕﹪±θ⁶≔÷⊖θ⁶θ

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

Nθ

Input n

Wθ«

Repeat until n is zero.

←I⊕﹪±θ⁶

Reduce -n modulo 6, then increment the result and output from right to left.

≔÷⊖θ⁶θ

Decrement n and integer divide it by 6.

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

Retina 0.8.2, 36 bytes

.+
$*_
+`(_*)\1{5}(_+)
$1$.2
T`7-1`d

Try it online! Link includes test cases. 1-indexed. Explanation:

.+
$*_

Convert to unary. (Retina 1 would save 2 bytes here.)

+`(_*)\1{5}(_+)
$1$.2

Convert to bijective base 6 by repeated divmod. Note that the use of + means that the extracted digit is always a number from 1 to 6 instead of 0 to 5 for regular base 6 conversion. ((_{6})* is faster but costs bytes extracting the quotient.)

T`7-1`d

Transpose the digits so that the 6s come first and the 1s last. (There are no 7s or 0s but this allows me to use the d shortcut.

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

Cubix, 22 bytes

This will output the sequence indefinitely. The general idea is that it has a base number which 6 - 1 is added to. For each add the result is output multiplied by 10, which is pushed to the bottom of the stack to be used later in the sequence. The base is then popped and the next base started.

..w.06+ONo*w;|uW!(pq;;

Try it online!

    . .
    w .
0 6 + O N o * w
; | u W ! ( p q
    ; ;
    . .

Watch It Run

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

C# (.NET Core), infinite printing, 181 180 88 bytes.

string[] s=new string[]{" "},t;int i,j,k,l=1;while(true){j=l;t=new string[l=6*j];for(i=6;i>0;i--)for(k=(6-i)*j;k<l;)t[k]=i+s[k++%j];
for(k=0;k<l;)System.Console.Write(t[k++]);s=t;}

Sadly it freezes repl.it instead of outputting properly in the infinite version as written (I believe it to be an error in repl.it, as it does not output as the program loops like it should), so anyone hoping to test needs a computer. If you add a read at the front of the loop it works in repl.it as well.

Outputs to the console, obviously.

On any finite system the code will most likely eventually crash with an out of memory error.

Reworked the code to use @dana 's lambda.

int f(int n)=>n-->0?f(n/6)*10+6-n%6:0;for(int i=0;++i>0;)System.Console.Write(f(i)+" ");

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I have no idea if I've golfed this well or not. \$\endgroup\$ – Stackstuck Feb 20 at 11:40
  • \$\begingroup\$ Saved one byte by removing unnecessary k++. \$\endgroup\$ – Stackstuck Feb 20 at 11:42
  • \$\begingroup\$ (also, I greatly welcome golfing assistance, I'm very new to this.) \$\endgroup\$ – Stackstuck Feb 20 at 11:52
  • 2
    \$\begingroup\$ Welcome :) If you are interested in golfing in C#, you may want to check out this post for some tips: codegolf.stackexchange.com/q/173/8340 \$\endgroup\$ – dana Feb 20 at 15:53
1
\$\begingroup\$

Forth (gforth), 63 bytes

: f recursive dup 6 < if 6 - abs 1 .r else 6 /mod 1- f f then ;

Try it online!

0-indexed outputs nth value

Explanation

If N is less than 6, output the absolute value of N - 6. Otherwise, get the quotient and remainder of dividing N by 6. Call the function recursively on the quotient, then call it on the remainder.

Code Explanation

: f                 \ start a new word definition
  recursive         \ declare that this word is recursive so we can call it from itself
  dup 6 <           \ check if n is less than 6
  if                \ if it is:
    6 - abs 1 .r    \ subtract 6, get the absolute value, then print with no appended space
  else              \ else if it's greater than 6:
    6 /mod          \ get the quotient and remainder of dividing n by 6
    1-              \ subtract 1 from the quotient (because we're 0-indexed)
    f               \ call f on the result
    f               \ call f on the remainder (shortcut to format and print, it's always < 6)
  then              \ end the if/else
;                   \ end the word definition
\$\endgroup\$
1
\$\begingroup\$

APL(NARS), 27 chars, 54 bytes

{0>⍵-←1:0⋄(6-6∣⍵)+10×∇⌊⍵÷6}

traslate the solution by dana https://codegolf.stackexchange.com/a/179980 in APL... test:

  f←{0>⍵-←1:0⋄(6-6∣⍵)+10×∇⌊⍵÷6}
  f 500
5625
  f¨750 1000 9329 9331 10000 100000
4531 3433 11112 666666 663633 6131233 
  f¨⍳9
6 5 4 3 2 1 66 65 64 
\$\endgroup\$
0
\$\begingroup\$

C#, print from start to n, ??? bytes

Credit to @dana for the lambda expression.

int f(int n)=>n-->0?f(n/6)*10+6-n%6:0;for(int i=0;i<int.Parse(a[0]);)System.Console.Write(f(++i)+" ");

Operation: Run with command line 0th argument equal to the integer you want to count to. (It should be noted that a[0] is a reference to the otherwise unmentioned command line args array, and I don't know how to count it.)

\$\endgroup\$
  • \$\begingroup\$ Since part of the code is a snipper instead of a full program or function, I assume you're using the Visual C# Interactive Compiler? Could you perhaps add a Try it online link with test code? PS: Your current byte-count is 102 \$\endgroup\$ – Kevin Cruijssen Feb 20 at 17:34
  • \$\begingroup\$ ah shit it isn't working what in the heck. \$\endgroup\$ – Stackstuck Feb 21 at 1:58

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.