26

Ruby, 26 bytes ->n{~-n*12-496/4**n%4+1/n} Try it online! Revised version adding 1/n and subtracting 496/4**n%4 to get the +1,-3,-3,-1 offset for the first 4 terms. Ruby, 32 bytes ->n{n>4?~-n*12:[0,1,9,21,35][n]} Try it online! After 4, the sequence settles down to (n-1)*12. See diagram below (the equilateral triangles have been distorted into ...


9

JavaScript (ES6), 23 bytes Based on Level River St's answer. n=>[1,5,13,7][--n]^n*12 Try it online! How? We compute \$(n-1)\times12\$ and adjust the first 4 values with a XOR. $$\begin{array}{c|c} n&1&2&3&4&5&6&7&8&9&10\\ \hline (n-1)\times12&0&12&24&36&48&60&72&84&96&...


9

05AB1E, 9 bytes <©12*3®cα Try it online! or try a test suite. < # input - 1 © # save to register 12* # multiply by 12 ® # push the register 3 c # binomial coefficient(3, input - 1) α # absolute difference With 0-indexing, this would be 7 bytes: 12*3Icα


8

JavaScript (ES6),  91 ... 76  74 bytes A recursive approach. This is very fast up to \$n=5\$. Computing \$a(6)\$ takes approximately 2 minutes on my laptop. n=>(g=a=>1+(h=i=>i&&h(i-1)+(a[i]^i&&a[i-1]?g(b=[...a,0],b[i]++):0))(n))`1` Try it online! How? Instead of explicitly building the sequences, we just keep track of ...


8

Pyth, 12 bytes f}hQ_XsjT;H1 Try it online! f: Count up from 1 to find the first number such that: X ... H1: Increment or insert the value 1 into the dictionary H at index sjT;: sum of base ten digits of current number _: values of dictionary }hQ: check whether input + 1 is contained within. The first time this is true, exactly input numbers must have the ...


8

Jelly,  39  37 bytes Soon to be crushed by MATL and, quite possibly, APL p`œc⁸ḣ1ạ§ỊẸʋƇ@;QɗƬƊṪṢƊƑƇŒṬZṚŒṪƲƬṂ$€QL Try it online! (It's pretty slow - a(6) took ~30 minutes locally!) To see them instead try this (L -> ŒṬ€G€j⁾¶¶). How? Builds all index lists, where each represents the locations of 1s on a grid of 1s and 0s, up to those for a square ...


7

Pyth, 13 bytes lfUI{T{.ysmRh Try it online! Let's go through this step by step, starting at the end: mRh: This autoexpands to mRhdQ. This means "Map the function m with right input hd over the input Q." Q is an integer (e.g. 3) so it gets automatically cast to a range (e.g. [0, 1, 2]). The m function is the map function. d is defined to be the input to ...


7

Jelly, 8 bytes ’3cạ×ʋ12 Try it online! How? ’3cạ×ʋ12 - Link: integer, n ’ - decrement (n-1) 1 2 3 4 5 6 7 ... 12 - twelve 12 12 12 12 12 12 12 12 ... ʋ - dyad: 3 - three 3 3 3 3 3 3 3 3 ... c - ...


7

Python, 31 bytes lambda n:n*12-11-(n>4or 5%-n%5) Try it online!


5

JavaScript (Node.js),  269 ... 250  243 bytes Rather slow for \$n\ge7\$, but it does find \$a(8)=704\$ in a bit more than 2 minutes on my laptop. f=(n,m=[...o=Array(w=n)],i=c=0)=>n?m.map((r,y)=>m.map((_,x,[...m])=>!i|1<<x&~r&(m[y+1]|r/2|r*2)&&f(n-1,m,m[y]|=1<<x)))|c:[0,0,0,0].some(_=>o[M=(m=m.map((_,y)=>...


4

Jelly, 18 bytes x`€FŒ!QJƑ$Ƈ¹Ƥ€ẎQL‘ Try it online! An initial attempt at a Jelly answer. A monadic link taking an integer and returning an integer. Too slow for n>3 on TIO. Explanation x`€ | Repeat each of 1..n itself times F | Flatten Œ! | Permutations $Ƈ | Keep those where the following ...


4

R, 113 106 98 bytes g=function(n,N=1:n,B=N*0){for(e in N[c(1,B)[-n-1]>0&(F=all(B<=N))])F=F+g(n,,'[<-'(B,e,B[e]+1));+F} Try it online! -9 bytes thanks to @RobinRyder -2 bytes using @Arnauld idea of storing occurrences table only (I just noticed that basically I was using the same approach, but creating the actual list too)


4

Jelly, 9 bytes ŻŒḂ€ḋṚ$HĊ A monadic Link accepting a non-negative integer which yields a non-negative integer. Try it online! How? Counts all pairs without the \$a\leq b\$ restriction, halves and rounds up. Note that the halved count is only a fraction if \$\frac n 2\$ is a palindrome and in such cases we want to count this \$a=b\$ pair. ŻŒḂ€ḋṚ$HĊ - ...


4

Jelly, 15 9 bytes ’3cạ×12$Ʋ Try it online! A monadic link taking \$n\$ as its argument and returning \$a(n)\$. Based on @LevelRiverSt’s clever Ruby answer so be sure to upvote that one too! Thanks to @Grimmy for saving 6 bytes! Explanation ’ | Subtract 1 Ʋ | Following as a monad 3c | - Number of ways of picking (n-1) items from 3 ...


4

APL (Dyalog Unicode), 14 bytesSBCS 12(|×-3!⍨⊢)-∘1 Try it online! Direct translation of Jonathan Allan's Jelly answer. Even the code structure is the same. Jelly is a golfy descendant of APL; if you want to learn Jelly, learn APL first! How it works 12(|×-3!⍨⊢)-∘1 ⍝ Monadic train, input: n 12( )-∘1 ⍝ Pass on to the inner function with left←12 and ...


4

brainfuck, 50 bytes >>>>>>+<++<<----<+<,-[>++++++++++++[[>+<-]<]>>-]>. Does i/o as raw byte values, like the others here.


4

Gaia, 13 10 bytes 1⟨┅Σ¦ṅC=⟩# Try it online! Straightforward implementation. Explanation: 1⟨ ⟩# | find the first 1 positive integers N where: C | the count of ṅ | the digital sum d(N) ┅Σ¦ | in the list [d(1)..d(N-1)] = | is equal to the (implicit) input


3

Python 2,  73 70  63 bytes lambda n:sum(`n-v`+`v`==(`v`+`n-v`)[::-1]for v in range(n/2+1)) Try it online! Note that: (string_a == reverse(string_a)) and (string_b == reverse(string_b)) is equivalent to reverse(string_a + string_b) == (string_b + string_a) (where + is concatenation)


3

JavaScript (ES6),  74  73 bytes n=>(g=a=>a>n-a?0:![a,n-a].some(n=>[...n+''].reverse().join``-n)+g(-~a))`` Try it online!


3

APL (Dyalog Unicode), 62 bytesSBCS Infinite printing full program, 63 bytes s←⍎' '~⍨⍕ {⎕←s⊢a←⊃⍵⋄b←1+a⋄t←∪(b,⍨⊃a)(a,⊃⌽b)b,1↓⍵⋄t[⍋s¨t]}⍣≡⊂1 3 Try it online! This one is pretty fast. Theoretically can be made faster using a min heap, but the challenge is code golf after all. How it works: the concept If we denote each item in the sequence as a list of ...


3

Perl 6, 26 bytes {$_*12-[-1,3,3,1][$_]}o*-1 Try it online! If this could be zero-indexed the o*-1 at the end can be removed. Returns (n-1)*12, offsetting the first 4 values.


3

Python 3, 40 36 bytes lambda n:~-n*12-(*n*[0],1,3,3,-1)[4] Try it online!


3

Jelly, 10 bytes D€§ċṪ$=ʋ1# Try it online! A monadic link taking an integer \$n\$ and returning \$a(n)\$. Explanation ʋ1# | Find the first integer x where the following is true; D€ | - Digits of 1..x § | - Sum each ċṪ$ | - Count the number equal to the tail (after popping tail) = | - Equal to (implicit original ...


3

05AB1E, 10 bytes ∞.Δ1Ÿ1öć¢Q Try it online!


3

APL (Dyalog Unicode), 27 bytesSBCS {⌈⌿+\∘.=⍨1⊥10⊥⍣¯1⍳20×⍵}⍳+∘1 Try it online! How it works {⌈⌿+\∘.=⍨1⊥10⊥⍣¯1⍳20×⍵}⍳+∘1 ⍝ Right argument: n { }⍳+∘1 ⍝ Find the first index of n+1 from... ⌈⌿+\∘.=⍨1⊥10⊥⍣¯1⍳20×⍵ ⍝ ... the list of cumulative count of own digit sum (sort of) 20×⍵ ⍝ Practical upper bound 20×...


2

05AB1E, 1 byte η Builtins ftw ¯\_(ツ)_/¯ Outputs as a list of prefixes. Try it online or verify all test cases. Explanation: η # Push a list of prefixes of the (implicit) input-string # (and output this list implicitly as result)


2

05AB1E, 19 18 17 bytes ÝDÅΓœ€Œ€`ÙʒÙāQ}g> Pretty slow (\$n=3\$ in about 5.5 0.5 seconds on TIO), but it works. Inspired by @isaacg's Pyth answer. I have the feeling this can definitely be golfed by at least a few bytes, though. -1 byte thanks to @ExpiredData. -1 byte and sped up by changing the order of some operations thanks to @Grimmy. Try it online ...


2

Raku, 94 bytes {1+set map ~*,grep {all .unique Z==^$_},.permutations>>[[\,] ^$_][*;*]}o{(^$_ Zxx 1..$_)[*;*]} Try it online! Times out for \$n>3\$, mostly since it is running the same filter for all prefixes of all permutations of the largest list (i.e. [1,2,2,3,3,3,...n]). This results in a lot of duplicates that have to filtered out later.


2

Ruby, 100 bytes Relatively simple answer similar to the Jelly solution, but it's able to calculate for n=4 without timing out on TIO (albeit taking 30 seconds to do so), which Jelly can't at time of writing. ->n{r=(1..n).flat_map{|j|[j]*j};i=0;r.sum{r.permutation(i+=1).uniq.count{|a|a.uniq==[*1..a.max]}}+1} Try it online!


2

Perl 6, 44 bytes {+grep {.flip eq[R,] $_},(^$_ Z($_...$_/2))} Try it online! Finds the number of pairs of numbers such that the reverse of the string representation is equal to the string representation of the reversed pair.


Only top voted, non community-wiki answers of a minimum length are eligible