New answers tagged

1

Pyth, 30 bytes J*4]1VQ=+J+eJsPP*M.t,PJ_PJ0;<J Try it online! Returns the first \$n\$ elements of the sequence. J*4]1VQ=+J+eJsPP*M.t,PJ_PJ0;<JQ # Full program, last Q = input (implicitly added) J*4]1 # J = 4 * [1] (=[1,1,1,1]) VQ # for N in range(Q): =+J # J += +eJ # J[-1] ...


0

C/C++, 70 69 67 bytes -1 bytes thanks to Jonathan. int a(int n){int k=2,s=0;while(++k<n)s+=a(k)*a(n+~k);return s?s:1;} Try it online!


1

Charcoal, 26 bytes F⁵⊞υ¹FN⊞υΣ✂E⮌υ×κ§υλ³→I§υ±⁴ Try it online! Link is to verbose version of code. Prints the 0-indexed nth number, although it calculates using 1-indexing internally. Explanation: F⁵⊞υ¹ Start with a[0] = a[1] = a[2] = a[3] = a[4] = 1. Yes, this is 1-indexed, but then with an extra zeroth value. That's code golf for you. FN Calculate an ...


1

Perl 5, 13 bytes say for 1..10 Example execution perl -E 'say for 1..10'


1

Forth (gforth), 99 81 bytes : f recursive dup 4 > if 0 over 3 do over 1- i - f i f * + loop else 1 then nip ; Try it online! Output is nth term and input is 1-indexed Edit: Saved 17 bytes by switching to xnor's formula. Saved another 1 byte by using 1-indexed Code Explanation : f \ start a new word definition recursive ...


1

Octave, 75 bytes f(f=@(a)@(n){@()sum(arrayfun(@(k)a(a)(k)*a(a)(n-2-k),2:n-1)),1}{2-(n>3)}()) Try it online! Captcha wanted to verify I was a human when posting this. To be honest, I'm not so sure. I should think this is the first challenge where I believe that the recursive anonymous approach is actually shorter than the loop-based approach, see below....


6

05AB1E, 17 13 bytes 4Å1λ£₁λ¨Â¦¦s¦¦*O+ Not shorter than the existing 05AB1E answer, but I wanted to try the recursive functionality of the new 05AB1E version as practice for myself. Could perhaps be golfed by a few bytes. EDIT: And it indeed can, see the recursive version of @Grimy's 05AB1E answer below, which is 13 bytes. Outputs the first \$n\$ items: ...


1

Ruby, 42 41 bytes f=->n{n<4?1:(4..n).sum{|i|f[i-1]*f[n-i]}} Try it online! 1-indexed (to save 1 byte)


5

Wolfram Language (Mathematica), 36 bytes Sum[#0@i#0[#-i-1],{i,3,#-1}]/. 0->1& Try it online! 1-indexed. The 2-indexed sequence is 4 bytes shorter: Sum[#0@i#0[#-i],{i,#-4}]/. 0->1&. Try it online!


0

Perl 5 -MList::Util=sum, 61 bytes sub a{my$b=-2+pop;$b<2||sum a($b+1),map{a($_)*a($b-$_)}2..$b} Try it online!


5

Haskell, 49 43 39 bytes a n=max(sum[a k*a(n-2-k)|k<-[2..n-1]])1 Try it online! For n<3 the sum is 0, so max ... 1 raises it to 1. Edit: -6 bytes thanks to @Jo King.


2

Japt, 19 17 16 bytes Outputs the nth term, 1-indexed. @Zí*Zz2)Ťx}g4Æ1 Try it @Zí*Zz2)Ťx}g4Æ1 :Implicit input of integer U @ :Function taking an array as an argument via parameter Z Zí : Interleave Z with Zz2 : Z rotated clockwise by 180 degrees (simply reversing would be a bye shorter but ...


20

Python, 51 bytes f=lambda n,k=2:n<3or k<n and f(k)*f(n-k-2)+f(n,k+1) Try it online! Simplifies the formula a bit: $$a(n) = \sum_{k=2}^{n-1} a(k)\cdot a(n-2-k)$$ $$ a(-1) = a(0)= a(1)= a(2) = 1$$


10

05AB1E, 14 13 11 bytes $ƒˆ¯Âø¨¨¨PO Try it online! Outputs the nth element, 0-indexed. $ # push 1 and the input ƒ # repeat (input+1) times ˆ # add the top of the stack (initially 1) to the global array ¯ # push the global array  # and a reversed copy of it ø # ...


1

Haskell, 65 bytes f a|a<4=1|z<-g[2..a]=sum$zipWith(*)z$reverse(1:g[0..a-4]) g=map f Try it online! You can use either f to get a single element of a sequence, or pass a list of values to g and get all the indexes for that list.


2

Haskell, 76 bytes 1:1:1:f[1,1,1] f(x:z)|y<-x+sum(zipWith(*)(init$init z)$reverse z)=y:f(y:x:z) Try it online!


4

Python 3, 59 bytes really inefficient, a(13) doesn't finish on TIO. a=lambda n,k=2:n<4or(n-k<2)*a(n-1)or a(k)*a(n-k-2)+a(n,k+1) Try it online!


9

Perl 6, 44 bytes {1,1,1,1,{sum @_[2..*]Z*@_[@_-4...0,0]}...*} Try it online! Anonymous code block that returns a lazy infinite sequence of values. This pretty much implements the sequence as described, with the shortcut that it zip multiplies all the elements so far after the second element with the reverse of the list starting from the fourth element and ...


3

Jelly, 17 bytes 1WṪ;+¥×Uṫ3SƲ;@Ʋ⁸¡ Try it online! A monadic link taking the zero-indexed \$n\$ and returning the list of generalized Catalan numbers from \$0\$ to \$n\$.


5

JavaScript (ES6), 42 bytes A port of xnor's solution. 0-indexed. f=(n,k=2)=>n<3||k<n&&f(k)*f(n+~++k)+f(n,k) Try it online! JavaScript (ES6),  83  75 bytes A faster, less recursive, but significantly longer solution. 0-indexed. f=(n,i,a=[p=1])=>a[n]||f(n,-~i,[...a,p+=(h=k=>k<i&&a[k]*a[i-++k]+h(k))(2)]) Try ...


2

APL (Dyalog Extended), 34 bytesSBCS -2 thanks to dzaima. Anonymous prefix lambda. {⍵≤3:1⋄+/(∇⍵-1),⍵(-×⍥∇¯2+⊢)¨4…⍵} Try it online!


1

Putt, 2 bytes X: X # Roman Numeral for 10 : # Ranger operator pushes [1..N] # Putt implicitly prints


0

R, 87 bytes Given n outputs a(n) j=scan();n=2;while(j-1){for(i in (n+1):(2*n)){n=ifelse(any(i%%2:(i-1)<1),n,i)};j=j-1};n Try it online! I'm still working on "Given n output a(1), a(2)... a(n)". I thought I could just modify this code slightly, but it seems more difficult than that.


0

Turing Machine Language, 199 bytes 0 * 1 r 1 1 _ _ r 2 2 * 2 r 3 3 _ _ r 4 4 * 3 r 5 5 _ _ r 6 6 * 4 r 7 7 _ _ r 8 8 * 5 r 9 9 _ _ r a a * 6 r b b _ _ r c c * 7 r d d _ _ r e e * 8 r f f _ _ r g g * 9 r h h _ _ r i i * 1 r j j * 0 r k Try it online!


14

JavaScript (Node.js),  578 ... 433  431 bytes f=(n,T=[B=[N=0,0,0,1,1]])=>!n||T.some(([x,y,q,m])=>B.some((p,d)=>m>>d&1&&((p=x+~-s[d],q=y+~-s[d+2],t=T.find(([X,Y])=>X==p&Y==q))?(q=t[3])&(p=D[d*3+t[4]])^p?t[f(n,T,t[3]|=p),3]=q:0:[0,1,2].map(t=>f(n-1,[...T,[p,q,-p-q,D[d*3+t],t]])))),s="2100122",D=Buffer("160).(...


0

Brain-Flak, 14 bytes {(({})[()])}{} Try it online! simply pushes the top stack value -1 until it reaches 0. Then it pops the zero and implicitly prints the stack


0

Clojure REPL, 12 bytes (range 1 11) I think this is self explanatory code


1

Keg, 5 bytes I am too lazy to change my answers to conform the new code page, unless I get notified. ėÏ_(. Push 10(saving a byte), take iota [10..0], remove last, and then print.


4

MarioLANG, 34 27 25 bytes +< :" + : + : + : + :! =# Try it online! Thanks to Jo King for -7 -9 bytes! code: Mario falls down, incrementing and saying his number five times, then he steps on the elevator, rides up and falls down again, incrementing and saying his number another five times. Then he walks left and falls out of the code. 14 bytes ...


2

Brian & Chuck, 17 bytes 1 1{? !{.+>-?>.-. Try it online! code: Brian: 1 1 ("1", 9, "1") constants {? restart Chuck's code Chuck: { Move Brian to the leftmost character .+ Print and increment character >- decrement counter ? if counter is greater than zero, switch to Brian (restart Chuck's code) >.-. ...


0

Javascript 45 Bytes alert("10charstrn".split("").map((e,i)=>i+1)) This is currently quite poor, but if there happens to be a JS constant which is an array of length 10, this could improve a lot.


0

Seed, 4232 4039 3981 bytes To be golfed. 11 ...


1

TacO, 7 bytes @%10 i try it online! The number 10 is the first branch of the looping construct %, so TacO runs the second branch, which just contains i, 10 times, giving i numbers 1-10.


1

k4, 28 24 bytes 0,+\"j"$2 xexp,/-1+|,\!: @Grimy's approach ported to k4 edit: -4 thanks to ngn!


1

C (clang), 73 bytes o,j,y;f(x){for(o=j=0;printf("%d ",o),x;o+=y+!y,y+=y+!y)j=!j?y=0,--x:--j;} Try it online! for(o=j=0;printf("%d ",o),x; o+=y+!y, y+=y+!y) // adds 1, 1+1=>2 , 2+2=> 4 .... sequence j=!j?y=0,--x:--j; // uses ternary instead of nested loop to decrement 'x' when 'j' go to 0


0

Haskell, 49 bytes g 1 g a b=b:g(a*b)([c|c<-[2..],1>mod(a*b+1)c]!!0) Try it online! Returns the infinite sequence as a lazy list. Explanation: g 1 -- Initialise the product as 1 g a b= -- Given the product and the current number b: ...


1

Perl 6, 33 32 bytes -1 byte thanks to nwellnhof {$_,{1+(2...-+^[*](@_)%%*)}...*} Try it online! Anonymous code block that takes a number and returns a lazy list. Explanation: { } # Anonymous codeblock ...* # That returns an infinite list $_, # Starting with the ...


1

JavaScript, 115 bytes f=(n,a=[],i=1)=>{for(;i++<n;)n%i||(a=a.concat(f(n/i).filter(e=>!(e[0]%i)).map(e=>[i].concat(e))));return n>1?a:[a]} I will write an explanation later


1

Wolfram Language (Mathematica), 78 76 72 71 67 bytes If[#>(p=1##2),Join@@If[i∣##,##~#0~i,{}]~Table~{i,2,#/p},{{##2}}]& Try it online! Recursive search tree. Brute force solution, 64 bytes: Union@Cases[Range@#~Tuples~#,{a__,__}/;1a==#&&a>=2&&1∣a:>{a}]& Trivial modification of my Mathematica solution to List all ...


3

Jelly, 17 bytes ÆfŒ!Œb€ẎP€€QḍƝẠ$Ƈ Try it online!


5

Haskell, 66 62 60 bytes f n=[n|n>1]:[k:l:m|k<-[2..n],l:m<-f$div n k,mod(gcd n l)k<1] Try it online!


2

JavaScript (V8),  73  70 bytes Prints the tuples in descending order \$(k_m,k_{m-1},...,k_1)\$. f=(n,d=2,a=[])=>n>1?d>n||f(n,d+1,a,d%a[0]||f(n/d,d,[d,...a])):print(a) Try it online! Commented f = ( // f is a recursive function taking: n, // n = input d = 2, // d = current divisor a = [] ...


0

Japt, 22 bytes â Åï c à f@¥XשXäv eÃâ Try it â Åï c à f@¥XשXäv eÃâ :Implicit input of integer U â :Divisors Å :Slice off the first element, removing the 1 ï :Cartesian product c :Flatten à :Combinations f ...


3

05AB1E, 13 bytes Òœ€.œP€`êʒüÖP Try it online! Ò # prime factorization of the input œ€.œ # all partitions P # product of each sublist €` # flatten ê # sorted uniquified ʒ # filter by: üÖ # pairwise divisible-by (...


1

05AB1E, 17 15 14 bytes ѦIиæʒPQ}êʒüÖP Very slow for larger test cases. -1 byte thanks to @Grimy. Try it online. Explanation: Ñ # Get all divisors of the (implicit) input-integer ¦ # Remove the first value (the 1) Iи # Repeat this list (flattened) the input amount of times # i.e. with input 4 we ...


2

APL (Dyalog Extended), 15 bytes This is a fairly simple implementation of the algorithm which uses Extended's very helpful prime factors builtin, ⍭. Try it online! {⍵,⊃⍭1+×/⍵}⍣⎕⊢⎕ Explanation {⍵,⊃⍭1+×/⍵}⍣⎕⊢⎕ ⊢⎕ First get the first prime of the sequence S from input. { }⍣⎕ Then we repeat the code another input number of times. ...


0

Hy, 44 bytes (defn a[p](print(get(str(*(+(int p)1)9))0))) Uses Grimy's method Try it online!


2

Brachylog, 27 bytes 1;0|⟦₅;2z^₍ᵐLtT&-₁↰+ᵐ↙T,L,0 Try it online! Outputs out of order and with duplicates. If that's not okay, tack do onto the end.


1

C (gcc), 54 53 bytes p;f(x,n){for(p=x;--n;p*=x)for(x=1;~p%++x;);return x;} Try it online! -1 byte thanks to ceilingcat


0

Oasis, 2 bytes Answer to the open exercise on the Oasis repo. +T Explanation Expanded program: bc+10 When + requires 1 parameter, it tries to calculate a(n-1). For the other parameter, it tries to calculate a(n-2). (Hence the expansion.) In addition, the T instruction expands to 10 in the program, which are the base test cases (a(0) is 0. a(1) is 1. ...


Top 50 recent answers are included