# Triple countdown sequence

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100...


Now we will make a new list, replacing each natural number n with a countdown from n to 1.

[1,2,1,3,2,1,4,3,2,1,5,4,3,2,1,6,5,4,3,2,1,7,6,5,4,3,2,1,8,7,6,5,4,3,2,1,9,8,7,6,5,4,3,2,1,10,9,8,7,6,5,4,3,2,1,11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1,13,12,11,10,9,8,7,6,5,4,3,2,1,14,13,12,11,10,9,8,7,6..


Now we will repeat the process.

[1,2,1,1,3,2,1,2,1,1,4,3,2,1,3,2,1,2,1,1,5,4,3,2,1,4,3,2,1,3,2,1,2,1,1,6,5,4,3,2,1,5,4,3,2,1,4,3,2,1,3,2,1,2,1,1,7,6,5,4,3,2,1,6,5,4,3,2,1,5,4,3,2,1,4,3,2,1,3,2,1,2,1,1,8,7,6,5,4,3,2,1,7,6,5,4,3,2,1,6..


And now we will do it a third time:

[1,2,1,1,1,3,2,1,2,1,1,2,1,1,1,4,3,2,1,3,2,1,2,1,1,3,2,1,2,1,1,2,1,1,1,5,4,3,2,1,4,3,2,1,3,2,1,2,1,1,4,3,2,1,3,2,1,2,1,1,3,2,1,2,1,1,2,1,1,1,6,5,4,3,2,1,5,4,3,2,1,4,3,2,1,3,2,1,2,1,1,5,4,3,2,1,4,3,2,1...


This is the triple countdown sequence.

Your task is to implement the triple countdown sequence. You may either take input and give the value of the sequence at that index, or you may simply output the terms starting from the beginning without stop.1

This is so answers will be scored in bytes with fewer bytes being the goal.

1: Both zero and one indexing are permitted. Outputting the terms without halt includes lazy structures which can produce an infinite number of terms.

• The 0-indexed position of the first $n$ in the sequence is $n(n-1)(n+1)(n+2)/24$, which is close to A000332, only with a different indexing. Sep 19 at 15:31
• The 0-indexed position of the second $n$ in the sequence is A145126. Sep 19 at 15:38
• Another interesting question would be the n-th countdown sequence. Sep 19 at 17:19
• @ykcul not so interesting I suppose, the trivial solution will be to just replace loop 3 times with n times Sep 19 at 17:35
• @wasif I meant take in n and output the nth countdown sequence. Sep 19 at 18:03

# APL (Dyalog Extended), 12 bytes

Returns the nth number in the series.

⊢⊃1(∊…¨⍨)⍣3⍳


Try it online!

⊢ the argument

⊃ picks the element from

1()⍣3⍳ repeat thrice with 1 as constant left argument and indices 1 through n as initial right argument:

∊enlist (flatten)

… the sequence

¨ for each

⍨ with swapped arguments, i.e. counting down from that number to 1

# 05AB1E, 6 bytes

∞3FLí˜


Try it online!

Infinitely outputs, thanks to Kevin Crujisen

# 05AB1E, 6 bytes

L3FLí˜


Try it online!

I doubt it could be beaten

• I don't think you can output the terms in this way..."You may either take input and give the value of the sequence at that index, or you may simply output the terms starting from the beginning without stop" Sep 19 at 19:07
• @ZaMoC Luckily that can easily be fixed by changing the leading L to ∞ to output an infinite list. Sep 19 at 21:17
• @KevinCruijssen thanks!! Sep 20 at 6:37
• @ZaMoC fixed !! Sep 20 at 6:37

-1 byte thanks to Wheat Wizard!
-1 byte thanks to pxeger!

[1..]>>=f>>=f>>=f
f x=[x,x-1..1]


Try it online!

• You can save 1 byte with do notation. Sep 19 at 15:47
• Simpler and a byte shorter: Try it online! Sep 19 at 16:09

# jq, 50 bytes

def f:[range(.)+1]|reverse[];
1|while(1;.+1)|f|f|f


Try it online!

funny how similar the second line is to the Zsh answer.

-1 from ovs.

• Infinite output fixes this while being a byte shorter: Try it online! Using range(.;0;-1) is a bit shorter than range+reverse as well and the newline is not necessary
– ovs
Sep 20 at 9:36

# Zsh, 37 bytes

c()xargs -i seq {} -1 1
seq inf|c|c|c

Attempt This Online!

Full program which outputs infinitely, one per line (ATO link includes a wrapper to halt after N terms).

"Implicit" "list" "flattening" FTW! (Zsh doesn't actually have any of these features - it's just text!)

c is a function that outputs the sequence from {} to 1 (with a step of -1) for each {} in the input (xargs -i). Then output "all" integers and pipe that to c three times.

# Husk, 6 bytes

!4¡ṁṫN


Try it online!

outputs an infinite list.

# JavaScript (V8), 60 bytes

for(a=1;;a++)for(i=a;--i;)for(j=i;--j;)for(k=j;--k;)print(k)


Try it online!

Boring trivial solution.

# Wolfram Language (Mathematica), 36 bytes

Do[Print[0;],{,∞},#,#,#]&@{,,1,-1}


Try it online!

Prints elements indefinitely.

A somewhat more interesting solution which generalizes to $$\n\$$-countdown sequences:

### Wolfram Language (Mathematica), 48 bytes

Nest[gArray[g,#,{#,1}]&,Print,3]@i~Do~{i,∞}


Try it online!

# PowerShell Core, 33 bytes

Outputs terms without stopping!

filter s{$_..1} for(){++$x|s|s|s}


Try it online! Note: the linked TIO limits the output

# Scala, 60 bytes

Stream.from(1)flatMap>flatMap>flatMap>
def> =(_:Int)to(1,-1)


Try it in Scastie!

A less trivial solution should be coming soon.

> is a function that takes an Int and returns a range going from that number to 1. We then take an infinite Stream starting at 1, and apply > to each of those numbers thrice, flattening each time.

# Raku, 33 bytes

(1..*,{.flatmap:{$_...1}}...*)[3]  Try it online! This is a lazy list of countdown sequences, where the first element is the list of natural numbers, the second is the countdown sequence, the third is the double countdown sequence, and the fourth is the triple countdown sequence, et cetera. The triple countdown sequence is the one at index 3, which is returned. # Wolfram Language (Mathematica), 45 bytes Flatten[r@r@r@Range@#][[#]]& r=Range[#,1,-1]&  Try it online! -23 bytes from @att • 45 bytes – att Sep 20 at 6:08 # R, 59 51 bytes [=for;while(F<-F+1)i[F:1,j[i:1,k[j:1,show(k)]]]  Try it online! Prints the resulting sequence infinitely. Less boring: ### R, 68 bytes !=unlist;s=sapply;while(F<-F+1)cat(!s(!s(F:1,seq,,-1),seq,,-1),"")  Try it online! # C++ (gcc), 101 bytes #include<cstdio> int a,x,y,z;main(){for(;;)for(x=a++;x--;)for(y=x;y--;)for(z=y;z;)printf("%d,",z--);}  Try it online! • 94 bytes Nov 22 at 8:06 # Jelly, 8 bytes r1)⁺⁺Fị@  Try it online! Because it is extremely cumbersome to produce infinite output in Jelly, it instead applies countdown 3 times to the input's range and outputs the input-th item. The part without indexing (6 bytes) ties with 05AB1E's equivalent. r1)⁺⁺Fị@ Monadic main link. Input = n r1) [x,x-1..1] for each number x (autorange) ⁺⁺ Do the above; do the above (no autorange) Fị@ Flatten and get the nth item  # Python 3, 72 bytes def c(x,i):i>0<x!=(print(x),c(x-1,i),c(x-1,i-1)) i=1 while 1:c(i,2);i+=1  Try it online! -19 bytes thanks to Wheat Wizard by using even more recursion -1 byte thanks to ovs • @WheatWizard Oh that i*x trick is very nice. And yes, that's a very nice save, thanks. Sep 19 at 15:39 • You can save another byte with chained comparison instead of the if statement: Try it online! – ovs Sep 19 at 15:52 • The output of the 73 byte version doesn't match the challenge; There is a 1 missing right at the start. The original program was fine – ovs Sep 19 at 16:01 • @ovs It looks fine to me? Anyway, thanks for the byte save Sep 19 at 16:13 • @hyper-neutrino yours starts 1 2 1 1 3 instead of 1 2 1 1 1 3 Sep 19 at 16:21 # JavaScript (V8), 51 bytes e=f=>i=>{for(;--i;)f(i)};e(_=>e(e(e(print)))(-_))  Try it online! # MathGolf, 9 bytes )╒3æ╒mx─§  Outputs the 0-based value. Try it online. Explanation: ) # Increase the (implicit) input-integer by 1 (work-around for input 0) ╒ # Pop and push a list in the range [1,input+1] 3æ # Loop 3 times, using four characters as inner code-block: ╒ # Convert each integer in the list to a [1,n] ranged list m # Map over each list: x # Reverse the list ─ # Flatten the list of lists § # After the loop, get the value at the (implicit) input as index # (after which the entire stack is output implicitly as result)  # C++ (gcc), 132123114113 112 bytes #include<argp.h> main(int a,int b){if(a<2)for(;;)main(2,a++);while(--b)if(a<4)main(a+1,b);else printf("%d,",b);}  Try it online! Thanks to @wheat-wizard for tip on removing all whitespace • 97 bytes Nov 22 at 8:12 # Julia 1.0, 56 bytes ~i=i:-1:1 !i=[.~i...;] for i=1:-1%UInt println.(!!!i)end  Try it online! # Japt, 13 bytes There's gotta be a better way than this. Outputs the nth term, 0-indexed. gÈc!õ1}g3NËôÄ  Try it # Perl 5, 48 bytes sub d{sub c{map{reverse 1..$_}@_}sub{c c c++$i}}  Try it online! Returns an anonymous sub that can be called repeatedly to obtain the next list of items in the sequence. Ungolfed and commented: sub d { sub c { # This is where the magic happens. c(1) returns (1), c(2) returns (2, 1). # c(1, 2, 3) returns (1, 2, 1, 3, 2, 1) return map {reverse 1 ..$_} @_;
}
# 3 nested calls to c() to get the triple countdown
return sub{c(c(c(++$i)))}; }  # Factor + combinators.extras lists lists.lazy, 58 bytes [ 1 lfrom [ [ 1 [a,b] >list ] lmap-lazy lconcat ] thrice ]  Running in the listener, as lmap-lazy postdates build 1525, the one TIO uses. Note that output has been limited to 30 so I could actually take the screenshot. This limitation is not present in the code itself. ## Explanation It's a quotation (anonymous function) that returns an infinite lazy list of the triple countdown sequence. • 1 lfrom An infinite lazy list of the natural numbers. • [ ... ] thrice Apply a quotation (to the natural numbers) three times. • [ ... ] lmap-lazy Use a quotation to map over a lazy list. • 1 [a,b] >list Create a list from an input number to one. e.g. 5 -> L{ 5 4 3 2 1 } • lconcat Concatenate a list of lists into a single list. # C (gcc), 69 bytes Nested Ternary operator solution. a, b and c are the triple countdown state from fastest to slowest, and d is the increasing count. a is printed for every step in the countdown. (make that a+1 thanks to AZTECCO Thanks to AZTECCO for -10 bytes!! And -2 more!!! It took me an embarrassingly long time to figure out what's going on with the last suggestion, that's some serious golfing. a,b,c,d;main(){for(;;a=!a?b=!b?c-=c?1:--d:b-1:a-1)printf("%d,",a+1);}  Try it online! # Swift, 103 bytes let a:([Int])->[Int]={$0.flatMap{(1...$0).reversed()}};(1...).forEach{a(a(a([$0]))).forEach{print(\$0)}}


Functional programming in Swift, excepting the print call, which obviously is non-pure.

a is a closure that converts an array of integers to an array of their countdown, which is called three times for every number.

Try it online!

• Welcome to the site! I'm not sure about swift but on TIO your output looks a little strange. It seems like you gave it 5 as input and it output several terms and then stopped. Sep 26 at 10:04
• @WheatWizard the code prints the triple countdown sequence for the first 5 natural numbers. Maybe I misunderstood the first half of the requirements You may either take input and give the value of the sequence at that index? Sep 26 at 10:08
• @WheatWizard fixed the code, thanks for the feedback! Sep 26 at 10:26

# Ruby, 48 bytes

x=0
loop{x-=1;eval"(x...0).map{|x|"*3+"p -x}}}"}


Try it online!

• outputs infinitely

We eval "(x...0).map{|x|"*3 which becomes a depth 3 loop

x is decremented thus negative so that we can use range x..0

Then we put -x

# Vyxal, 9 bytes

ɾṘ3(vɾf)Ṙ


Try it Online!

# Charcoal, 36 bytes

Ｎθ≔⁰ηＷ›θＬυ«≦⊖ηＦ…η⁰Ｆ…κ⁰Ｆ…λ⁰⊞υμ»Ｉ±§υ⊖θ


Try it online! Link is to verbose version of code. Outputs the 1-indexed nth value. Explanation: Based on @AZTECCO's answer, generates the triple countup sequence for the negated natural numbers, and then negates the final value to produce the desired result.

Ｎθ


Input n.

≔⁰η


Ｗ›θＬυ«


Repeat until we have at least n terms.

≦⊖η


Get the next negated natural number.

Ｆ…η⁰Ｆ…κ⁰Ｆ…λ⁰⊞υμ


Generate the triple countup for that number.

»Ｉ±§υ⊖θ


Output the nth term, negating to give the desired result.

# Burlesque, 14 bytes

r1{m{ro^p}}3E!


Try it online!

r1    # Range 1..inf
{
m{   # Apply to each
ro  # Range from 1..N
^p  # Push in reverse
}
}
3E!   # Run 3 times


# Rust, 77 76 bytes

|i|{let g=|n|(1..=n).rev();(1..).flat_map(g).flat_map(g).flat_map(g).nth(i)}


Try it online!

-1 bytes thanks to ..=n trick

Takes i as a usize, returns the ith item in the sequence.
Conveniently returns it as an Option<i32> monad, so if you somehow pass an i greater than the length of the sequence (impossible because it's an infinite sequence), it will return None.
Relevant meta ruling on returning monads

See below for explanation of how it generates the sequence.

# Rust, 9490 89 bytes

let g=|n|(1..=n).rev();for i in(1..).flat_map(g).flat_map(g).flat_map(g){print!("{} ",i)}


Try it online!

-1 bytes thanks to ..=n trick
(1..) creates an infinite range starting at 1.
flat_map(g) is equivalent to map(g).flatten().
g in this case generates a range from n to 1 inclusive. Rust ranges are strictly ascending, so we have to do (1..=n).rev().
=n is used to tell Rust that the max here is inclusive, as it's a byte shorter than n+1.
I do wish there were a shorter way to call flat_map(g) three times without literally calling it three times...