27
\$\begingroup\$

Let's start with the natural numbers

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

\$\endgroup\$
6
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Arnauld
    Sep 19, 2021 at 15:31
  • 1
    \$\begingroup\$ The 0-indexed position of the second \$n\$ in the sequence is A145126. \$\endgroup\$
    – Arnauld
    Sep 19, 2021 at 15:38
  • \$\begingroup\$ Another interesting question would be the n-th countdown sequence. \$\endgroup\$
    – ophact
    Sep 19, 2021 at 17:19
  • \$\begingroup\$ @ykcul not so interesting I suppose, the trivial solution will be to just replace loop 3 times with n times \$\endgroup\$
    – Wasif
    Sep 19, 2021 at 17:35
  • \$\begingroup\$ @wasif I meant take in n and output the nth countdown sequence. \$\endgroup\$
    – ophact
    Sep 19, 2021 at 18:03

32 Answers 32

6
\$\begingroup\$

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

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

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

\$\endgroup\$
4
  • 3
    \$\begingroup\$ 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" \$\endgroup\$
    – ZaMoC
    Sep 19, 2021 at 19:07
  • \$\begingroup\$ @ZaMoC Luckily that can easily be fixed by changing the leading L to to output an infinite list. \$\endgroup\$ Sep 19, 2021 at 21:17
  • \$\begingroup\$ @KevinCruijssen thanks!! \$\endgroup\$
    – Wasif
    Sep 20, 2021 at 6:37
  • \$\begingroup\$ @ZaMoC fixed !! \$\endgroup\$
    – Wasif
    Sep 20, 2021 at 6:37
6
\$\begingroup\$

Haskell, 34 33 32 bytes

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

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

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ You can save 1 byte with do notation. \$\endgroup\$
    – Wheat Wizard
    Sep 19, 2021 at 15:47
  • 4
    \$\begingroup\$ Simpler and a byte shorter: Try it online! \$\endgroup\$
    – pxeger
    Sep 19, 2021 at 16:09
5
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – ovs
    Sep 20, 2021 at 9:36
4
\$\begingroup\$

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.

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

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.

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

Husk, 6 bytes

!4¡ṁṫN

Try it online!

outputs an infinite list.

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

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!

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

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

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

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

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.

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

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.

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

Wolfram Language (Mathematica), 45 bytes

Flatten[r@r@r@Range@#][[#]]&
r=Range[#,1,-1]&

Try it online!

-23 bytes from @att

\$\endgroup\$
1
  • \$\begingroup\$ 45 bytes \$\endgroup\$
    – att
    Sep 20, 2021 at 6:08
2
\$\begingroup\$

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!

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

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!

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

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.

enter image description here

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

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!

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

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!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Sep 26, 2021 at 10:04
  • \$\begingroup\$ @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? \$\endgroup\$
    – Cristik
    Sep 26, 2021 at 10:08
  • \$\begingroup\$ @WheatWizard fixed the code, thanks for the feedback! \$\endgroup\$
    – Cristik
    Sep 26, 2021 at 10:26
2
\$\begingroup\$

Nibbles, 6 bytes

<$/, 3,~.@\, 

explanation

<$      # first int input elements
  /     # fold
   ,3   # [1,2,3] (any list of length 3 would work)
   ,~   # [1,2,...inf] (initial value to fold)
   .@   # map on accumulator
     \, # reverse of list from 1 to
        # implicit $ (the element of list to map)
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
5
  • \$\begingroup\$ @WheatWizard Oh that i*x trick is very nice. And yes, that's a very nice save, thanks. \$\endgroup\$
    – hyper-neutrino
    Sep 19, 2021 at 15:39
  • \$\begingroup\$ You can save another byte with chained comparison instead of the if statement: Try it online! \$\endgroup\$
    – ovs
    Sep 19, 2021 at 15:52
  • \$\begingroup\$ 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 \$\endgroup\$
    – ovs
    Sep 19, 2021 at 16:01
  • \$\begingroup\$ @ovs It looks fine to me? Anyway, thanks for the byte save \$\endgroup\$
    – hyper-neutrino
    Sep 19, 2021 at 16:13
  • 6
    \$\begingroup\$ @hyper-neutrino yours starts 1 2 1 1 3 instead of 1 2 1 1 1 3 \$\endgroup\$
    – pxeger
    Sep 19, 2021 at 16:21
1
\$\begingroup\$

JavaScript (V8), 51 bytes

e=f=>i=>{for(;--i;)f(i)};e(_=>e(e(e(print)))(-_))``

Try it online!

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

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

C++ (gcc), 132 123 114 113 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

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

Julia 1.0, 56 bytes

~i=i:-1:1
!i=[.~i...;]
for i=1:-1%UInt println.(!!!i)end

Try it online!

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

Japt, 13 bytes

There's gotta be a better way than this.

Outputs the nth term, 0-indexed.

gÈc!õ1}g3NËôÄ

Try it

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

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

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

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

Vyxal, 9 bytes

ɾṘ3(vɾf)Ṙ

Try it Online!

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

Charcoal, 36 bytes

Nθ≔⁰ηW›θLυ«≦⊖ηF…η⁰F…κ⁰F…λ⁰⊞υμ»I±§υ⊖θ

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.

Nθ

Input n.

≔⁰η

Start with no terms.

W›θLυ«

Repeat until we have at least n terms.

≦⊖η

Get the next negated natural number.

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

Generate the triple countup for that number.

»I±§υ⊖θ

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

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

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