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 code-golf 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.
-
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– Arnauld2021年09月19日 15:31:55 +00:00Commented Sep 19, 2021 at 15:31
-
1\$\begingroup\$ The 0-indexed position of the second \$n\$ in the sequence is A145126. \$\endgroup\$Arnauld– Arnauld2021年09月19日 15:38:20 +00:00Commented Sep 19, 2021 at 15:38
-
\$\begingroup\$ Another interesting question would be the n-th countdown sequence. \$\endgroup\$ophact– ophact2021年09月19日 17:19:13 +00:00Commented 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– wasif2021年09月19日 17:35:23 +00:00Commented Sep 19, 2021 at 17:35
-
\$\begingroup\$ @wasif I meant take in n and output the nth countdown sequence. \$\endgroup\$ophact– ophact2021年09月19日 18:03:01 +00:00Commented Sep 19, 2021 at 18:03
32 Answers 32
APL (Dyalog Extended), 12 bytes
Returns the nth number in the series.
⊢⊃1(∊... ̈⍨)⍣3⍳
⊢
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í ̃
Infinitely outputs, thanks to Kevin Crujisen
05AB1E, 6 bytes
L3FLí ̃
I doubt it could be beaten
-
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– ZaMoC2021年09月19日 19:07:34 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2021年09月19日 21:17:57 +00:00Commented Sep 19, 2021 at 21:17 -
\$\begingroup\$ @KevinCruijssen thanks!! \$\endgroup\$wasif– wasif2021年09月20日 06:37:14 +00:00Commented Sep 20, 2021 at 6:37
-
\$\begingroup\$ @ZaMoC fixed !! \$\endgroup\$wasif– wasif2021年09月20日 06:37:23 +00:00Commented Sep 20, 2021 at 6:37
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]
-
2\$\begingroup\$ You can save 1 byte with do notation. \$\endgroup\$2021年09月19日 15:47:33 +00:00Commented Sep 19, 2021 at 15:47
-
4\$\begingroup\$ Simpler and a byte shorter: Try it online! \$\endgroup\$pxeger– pxeger2021年09月19日 16:09:55 +00:00Commented Sep 19, 2021 at 16:09
jq, 50 bytes
def f:[range(.)+1]|reverse[];
1|while(1;.+1)|f|f|f
funny how similar the second line is to the Zsh answer.
-1 from ovs.
-
1\$\begingroup\$ Infinite output fixes this while being a byte shorter: Try it online! Using
range(.;0;-1)
is a bit shorter thanrange
+reverse
as well and the newline is not necessary \$\endgroup\$ovs– ovs2021年09月20日 09:36:17 +00:00Commented Sep 20, 2021 at 9:36
Zsh, 37 bytes
c()xargs -i seq {} -1 1 seq inf|c|c|c
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 seq
uence 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.
JavaScript (V8), 60 bytes
for(a=1;;a++)for(i=a;--i;)for(j=i;--j;)for(k=j;--k;)print(k)
Boring trivial solution.
Wolfram Language (Mathematica), 36 bytes
Do[Print[0;],{,∞},#,#,#]&@{,,1,-1}
Prints elements indefinitely.
A somewhat more interesting solution which generalizes to \$n\$-countdown sequences:
Wolfram Language (Mathematica), 48 bytes
Nest[gArray[g,#,{#,1}]&,Print,3]@i~Do~{i,∞}
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
Jelly, 8 bytes
r1)++Fị@
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
Scala, 60 bytes
Stream.from(1)flatMap>flatMap>flatMap>
def> =(_:Int)to(1,-1)
(削除) 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]
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]&
-23 bytes from @att
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--);}
-
\$\begingroup\$ 94 bytes \$\endgroup\$ceilingcat– ceilingcat2021年11月22日 08:06:51 +00:00Commented Nov 22, 2021 at 8:06
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);}
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.
-
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\$2021年09月26日 10:04:46 +00:00Commented 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– Cristik2021年09月26日 10:08:11 +00:00Commented Sep 26, 2021 at 10:08 -
\$\begingroup\$ @WheatWizard fixed the code, thanks for the feedback! \$\endgroup\$Cristik– Cristik2021年09月26日 10:26:12 +00:00Commented Sep 26, 2021 at 10:26
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)
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
-19 bytes thanks to Wheat Wizard by using even more recursion
-1 byte thanks to ovs
-
\$\begingroup\$ @WheatWizard Oh that
i*x
trick is very nice. And yes, that's a very nice save, thanks. \$\endgroup\$2021年09月19日 15:39:23 +00:00Commented 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– ovs2021年09月19日 15:52:40 +00:00Commented 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– ovs2021年09月19日 16:01:48 +00:00Commented Sep 19, 2021 at 16:01
-
\$\begingroup\$ @ovs It looks fine to me? Anyway, thanks for the byte save \$\endgroup\$2021年09月19日 16:13:14 +00:00Commented 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– pxeger2021年09月19日 16:21:55 +00:00Commented Sep 19, 2021 at 16:21
MathGolf, 9 bytes
)╒3æ╒mx─§
Outputs the 0-based value.
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), (削除) 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);}
Thanks to @wheat-wizard for tip on removing all whitespace
-
\$\begingroup\$ 97 bytes \$\endgroup\$ceilingcat– ceilingcat2021年11月22日 08:12:07 +00:00Commented Nov 22, 2021 at 8:12
Perl 5, 48 bytes
sub d{sub c{map{reverse 1..$_}@_}sub{c c c++$i}}
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)))};
}
Ruby, 48 bytes
x=0
loop{x-=1;eval"(x...0).map{|x|"*3+"p -x}}}"}
- 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
Charcoal, 36 bytes
Nθ≔0ηW›θLυ«≦⊖ηF...η0F...κ0F...λ0⊞υμ»I±§υ⊖θ
Try it online! Link is to verbose version of code. Outputs the 1-indexed n
th 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
.
≔0η
Start with no terms.
W›θLυ«
Repeat until we have at least n
terms.
≦⊖η
Get the next negated natural number.
F...η0F...κ0F...λ0⊞υμ
Generate the triple countup for that number.
»I±§υ⊖θ
Output the n
th term, negating to give the desired result.
Burlesque, 14 bytes
r1{m{ro^p}}3E!
r1 # Range 1..inf
{
m{ # Apply to each
ro # Range from 1..N
^p # Push in reverse
}
}
3E! # Run 3 times