# Concentric rings on a snub square tiling

This challenge takes place on the snub square tiling.

Start by choosing any triangle, and color it $$\c_1\$$. Next, find all tiles which touch this triangle at any vertex, and color them $$\c_2\$$. Next, find all tiles which share a vertex with any $$\c_2\$$-colored tile, and color these $$\c_3\$$. Continue this process ad infinitum.

# Initial terms

The sequence begins

  a(1) = 1
a(2) = 9
a(3) = 21
a(4) = 35


Notice:

• a(1) = 1 corresponds to the red triangle;
• a(2) = 9 corresponds to the number of tiles in the second, orange layer;
• a(3) = 21 corresponds to the number of tiles in the third, green layer; and so on.

(Note, this sequence is now in the OEIS; OEIS sequence A296368 is closely related.)

# Challenge

Your goal is to write a program that takes in a positive integer n and returns the number of tiles that are colored $$\c_n\$$, (i.e. the number of tiles in the $$\n\$$-th layer.) This is a challenge, so the shortest code in bytes wins.

• This looks like a lion! +1
– RGS
Feb 4, 2020 at 21:42
• Can this be zero indexed?
– Jo King
Feb 4, 2020 at 22:23
• @JoKing, I'd like to keep it one-indexed—partly because folks have already submitted solutions with that assumption. Feb 4, 2020 at 22: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 45 degree isosceles triangles and the entire diagram rotated 45 degrees, but it remains topologically equivalent.)

• Really nice job :D
– RGS
Feb 4, 2020 at 23:24
• Well done on finding a way to simplify the topology and providing a closed formula for n>4! Feb 5, 2020 at 17:52

# 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&108\\ \hline \text{XOR}&1&5&13&7&\color{grey}0&\color{grey}0&\color{grey}0&\color{grey}0&\color{grey}0&\color{grey}0\\ \hline a(n)&1&9&21&35&48&60&72&84&96&108 \end{array}$$

# 05AB1E, 9 bytes

<©12*3®cα

<           # 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α


# 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  ...
3       -   three                3                    3   3   3   3   3   3   3  ...
c      -   choose               3C(n-1)              1   3   3   1   0   0   0  ...
×    -   multiply             (n-1)*12             0  12  24  36  48  60  72  ...
ạ     -   absolute difference  |3C(n-1)-(n-1)*12|   1   9  21  35  48  60  72  ...

• Ah, cool that Jelly has a way to avoid duplicating ’. I still have no idea how any of it works =p Feb 4, 2020 at 23:34
• Yeah even with Lynn's excellent tutorial and its Chains section, it's still a tricky beast :) Feb 4, 2020 at 23:43

# Python, 31 bytes

lambda n:n*12-11-(n>4or 5%-n%5)


Try it online!

# 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 right←n-1
×             ⍝ left × right
-            ⍝ minus
3!⍨⊢        ⍝ binomial(3, right)
|              ⍝ absolute value of the above


# 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 ạ$  | - Absolute difference from:
×12   |   - Multiply (n-1) × 12

• Here’s 9 bytes Feb 4, 2020 at 23:17
• @Grimmy thanks! I’d missed that because of the difference for n=1 being -1, though of course with absolute difference that doesn’t matter. Feb 4, 2020 at 23:24
• @Grimmy I was trying to golf exactly that, and now managed it :) Feb 4, 2020 at 23:31

# brainfuck, 50 bytes

>>>>>>+<++<<----<+<,-[>++++++++++++[[>+<-]<]>>-]>.


Does i/o as raw byte values, like the others here.

• You can remove leading >> by using the implementation available on TIO. Feb 6, 2020 at 1:46
• That's a nonstandard language extension that I'd like to see stamped out. I'm not a fan of the fragmentation of brainfuck into incompatible dialects, and I work against that where I can. Feb 6, 2020 at 2:16

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

# Python 3, 40 36 bytes

lambda n:~-n*12-(*n*[0],1,3,3,-1)[4]


Try it online!

# Python 3, 39 bytes

lambda n:n>4and~-n*12or[1,9,21,35][n-1]


Try it online!

Total rip of Level River St's answer so upvote him.

# Python, 38 bytes

lambda n:n*12-11-([1]*n+[2,4,4,0])[-n]


Try it online!

# J, 14 bytes

12(*|@-3!~])<:


Try it online!

A J port of Bubbler's APL answer, so it's also a port of Jonathan Allan's Jelly answer.

# C (gcc), 34 33 32 bytes

Saved a byte thanks to Grimmy!!!

f(n){n=--n<4?"!)5C"[n]-32:n*12;}


Try it online!

• 32 Feb 5, 2020 at 10:20
• @Grimmy Nice - takes care of all the n-1s with a decrement up front. Thanks! :-) Feb 5, 2020 at 11:52

# brainfuck, 78 bytes

++++[->+++<]>>>+>--->--->-<<<<<<,-[->>>[+]<[->+<]<[->+>+<<]<[->+<]>]>>[->+<]>.


You can try it online over at TIO (the input is a line-feed (ascii 10) and the output is an l (ascii 108))

You can also try the verbose code at this online interpreter where input can be inserted as decimals, e.g. \6 gives 6 as input. After running, you can hit the "view memory" button and check the value of the output cell in bold, to ensure the result is right.

# brainfuck, 61 bytes

,-[>++<-[>+++<-[>+++>++<<-[>+++>+<<-[>+++<-]]]]]>[>++++<-]>+.


Input/output as character codes (meta).

• Good work on this one!
– RGS
Feb 6, 2020 at 0:21

# Stax, 10 bytes

Å²@8Ω@╩vä3


Run and debug it

# Rust - 52 bytes

let m=|n|(match n{1=>1,2=>9,3=>21,4=>35,_=>n}-1)*12;


oh gosh.. it loses even to Brainf***