# Clark's Triangle

Ok I've been on a bit of a triangle kick recently so here's another one.

Clark's Triangle is a triangle where the leftmost entry of each row is 1 and the rightmost entries are made up of multiples of 6 which increase as the row number increases. Here's a visualization

     1 6
1 . 12
1 . . 18
1 . . . 24
1 . . . . 30
1 . . . . . 36


Just like Pascal's Triangle all other entries are the sum of the numbers to their upper right and upper left.

Here are the first few rows filled in

          1   6
1   7  12
1   8  19  18
1   9  27  37  24
1  10  36  64  61  30
1  11  46  100 125 91  36


Given a row number (starting from the top) and an column number (starting from the first non-zero item on that row) output the value at that particular cell. Both inputs may be either 1 or 0 indexed (you may mix and match if you desire). Out of the bounds of the triangle is undefined and you may do whatever you wish when queried for these values.

This is , the goal is to minimize the number of bytes in your solution.

OEIS A046902

• Can we build a solution with zero in the first row? like in the OEIS sequence Jul 6, 2017 at 14:42
• @JörgHülsermann Since that is out of bounds for the triangle defined here you may do whatever you want. Jul 6, 2017 at 14:43

# MATL, 15 bytes

[lBB]i:"TTY+]i)


First input is 0-based row; second is 1-based column.

Try it online!

### Explanation

[lBB]   % Push [1 6 6]
i       % Input: row number (0-based)
:"      % Repeat that many times
TT    %   Push [1 1]
Y+    %   Convolution, increasing size. This computes the sum of overlapping
%   pairs, including the endpoints. So for example [1 6 6] becomes
%   [1 7 12 6], which will later become [1 8 19 18 6], ...
]       % End
i       % Input: column number (1-based)
)       % Use as index. Implicit display


# Pascal, 132 bytes

function f(n,k:integer):integer;begin if k=1 then f:=1 else if k>n then f:=6*n else if k<0 then f:=0 else f:=f(n-1,k-1)+f(n-1,k)end;


Try it online!

1-indexed.

• Pascal's triangle! Jul 6, 2017 at 20:57

# CJam, 22 18 bytes

-4 bytes thanks to Martin Ender

X6_]ri{0X$+.+}*ri=  Input is (0-based row) (0-based column) Try it online! ### Explanation X6_] e# Push the list [1 6 6]. This is the first row, but each row will have an extra 6 at e# the end, which is out of bounds. ri e# Push the first input as an integer. { e# The following block calculates the next row given a row on top of the stack: 0X$+ e#  Copy the top list on the stack and prepend 0.
.+   e#  Element-wise addition with the list before prepending 0. This adds each element of
e#  with the one to its left, except the initial 1 gets added to 0 and the final number
e#  gets added to the out-of-bounds 6. The out-of-bounds 6 is unchanged since one list
e#  is longer.
}*    e# Run this block (row index) times.
ri=   e# Get the (column index)th item of the final list.

• A different technique for getting pairwise sums is to shift one copy left and use .+. Normally that has the problem that it retains the trailing element without summing it (which costs bytes to remove), but in this case that actually saves bytes because then you don't need to add a 6 on every iteration. You can save even more bytes because shifting left is free if you only prepend the 0 to one copy: X6_]ri{0X$+.+}*ri= Jul 6, 2017 at 15:08 • _0\+ instead of 0X$+ is the same byte count if you prefer. Jul 6, 2017 at 15:08
• @MartinEnder Oh I see, you get an extra 6 at the end of each row that's out of bounds so it doesn't matter. Clever, thanks. Jul 6, 2017 at 15:11

# C#, 157 bytes

using System.Linq;(b,c)=>{var f=new[]{1,6};for(;c>0;c--){int s=f.Length;f=new int[s+1].Select((e,i)=>i<1?1:i==s?f[s-1]+6:f[i-1]+f[i]).ToArray();}return f[b];


Try it online

# Python 2, 67 bytes

a,b=input()
x=[1,6]
exec"x=map(sum,zip(+x,x+));"*a
print x[b]


Try it online!

Brute-force approach, calculate the ath row, and then print the bth number, both inputs are 0-based

# Python 3, 6460 52 bytes

f=lambda r,c:c<2or c>r and r*6or f(r-1,c-1)+f(r-1,c)


Try it online!

Recursive solution using 1-indexing. Outputs "True" instead of 1 for the sake of golfing.

Thanks to:

• @totallyhuman for saving 4 bytes!
• @Rod for saving 8 bytes!
• 60 bytes Jul 6, 2017 at 14:44
• 52 bytes replacing the if/else with boolean operators and with a more flexible output
– Rod
Jul 6, 2017 at 14:54
• @Rod, this is a brilliant solution. I'm still trying to wrap my head around why it works. I am still fairly new here (this is only my second answer on the site), so I'm unsure on the protocol: should I include your revision in my answer even though you switched from Python 3 to 2? Jul 6, 2017 at 15:03
• @icosahedron the python version is irrelevent in that case, so you don't have to mind it. generally, switching between python versions to exploit features is considered OK. Jul 6, 2017 at 15:20
• @Uriel thank you for the clarification. Jul 6, 2017 at 15:32

n#1=1
n#m|m>n=6*n
n#m=(n-1)#(m-1)+(n-1)#m


Try it online!

Call using n # m where n is the row number and m is the column number, both 1-indexed.

# Husk, 15 12 bytes

!!¡SJẊ+d166


Try it online!

-3 bytes from Dominic Van Essen.

Same method as the MATL answer, except it uses an infinite list of rows of Clark's triangle.

• 12 bytes using J and recycling the d166... Oct 22, 2020 at 12:41

# Mathematica, 32 bytes

b=Binomial;b[#,#2-1]6+b[#-1,#2]&


input

[row,column]
[1-indexed,0-indexed]

# Scala, 75 bytes - Brute-force solution

n=>k=>2.to(n)./:(Seq(1,6))((s,i)=>1+:s.sliding(2).map(_.sum).toSeq:+6*i)(k)


Try it online!

# Scala, 88 bytes - Recursive solution

n=>k=>{def g(m:Int,l:Int):Int=if(m==l)m*6 else if(l<1)1 else g(m-1,l-1)+g(m-1,l)
g(n,k)}


Try it online!

Thanks to Lynn for pointing out the obvious and saving me 4 bytes.

n#1=1
n#m|m>n=6*n|q<-n-1=q#(m-1)+q#m


Try it online!

Works like this answer but 1 byte 5 bytes shorter.

• Hint: You can combine the last two lines into one with guards, and save some bytes.
– Lynn
Oct 21, 2020 at 22:06
• @Lynn Gah! That feels so obvious now. Oct 22, 2020 at 14:58

# K (oK), 28 18 bytes

{(x{+':x,6}/1 6)y}


Try it online!

Rows and columns are both 0-indexed. Out of bounds return null.

# Desmos, 11871 55 bytes

Saved a whopping 47 bytes thanks to @ovs!

$$\f(r,c)=\sum_{k=c-2}^c(6-5*0^{c-k})/k!\prod_{n=r-k+1}^rn\$$

f(r,c)=\sum_{k=c-2}^c(6-5*0^{c-k})/k!\prod_{n=r-k+1}^rn


Both the row and column are 0-indexed.

• This can be expressed quite a bit shorter as a sum. I think this works: f(r,c)=\sum_{k=0}^2\frac{(6-5*0^k)\prod_{n=0}^{c-1-k}(r-n)}{(c-k)!}
– ovs
Oct 24, 2020 at 23:23
• @ovs Nice! I was struggling with getting the 1, 6, 6, and didn't think about 0^k
– user
Oct 24, 2020 at 23:32

# R, 77 bytes

Reduce(function(x,y)zoo::rollsum(c(0,x,6),2),double(scan()-1),c(1,6))[scan()]


Requires the zoo library; reads from stdin (the inputs separated by two newlines) and returns the value, with NA for out of bounds selections.

Try it online!

• I know this is >3 years old, but you could save 7 bytes by taking the first input value as 0-indexed instead of 1-indexed, and replacing double(scan()-1) with !1:scan(). Oct 22, 2020 at 13:34

## JavaScript (ES6), 38 bytes

f=(r,c)=>c?r>c?f(--r,c)+f(r,--c):r*6:1


Crashes for negative columns, and returns multiples of six for negative rows or overlarge columns.

# C# (.NET Core), 44 bytes

f=(c,r)=>c<=1?1:c>r?6*r:f(c-1,r-1)+f(c,r-1);


Takes column then row, both 1-indexed. Can take row then column by swapping the inputs: (r,c). Will return row * 6 for coordinates outside the bounds on the right (i.e. column > row + 1), and 1 for coordinates outside the bounds on the left (i.e. column < 1).

# PHP, 64 bytes

recursive function

rows 1-indexing columns 0-indexing

Output for row=0 and column=0 is 0 like in the OEIS sequence

function f($r,$c){return$c-$r?$c?f($r-=1,$c-1)+f($r,$c):1:$r*6;}


Try it online!

# PHP, 126 bytes

rows 1-indexing columns 0-indexing

Output for row=0 and column=0 is 0 like in the OEIS sequence

for(;$r<=$argv;$r++)for($z++,$c=~0;++$c<$z;)$t[+$r][$c]=$c<$r?$c?$t[$r-1][$c-1]+$t[$r-1][$c]:1:$r*6;echo$t[$r-1][\$argv];


Try it online!

# Jelly, 13 bytes

,"’U0¦c/x6,1S


A monadic link taking a list of [row, entry] (0-indexing for entries, 1-indexing for rows), returning the value.

Try it online!

### How?

,"’U0¦c/x6,1S - Link: list of numbers, [row, entry]
’           - decrement     -> [row-1, entry-1]
"            - zip with:
,             -   pair        -> [[row, row-1], [entry, entry-1]]
¦        - sparse application of:
U          -   upend
0         - for indexes: 0 -> [[row, row-1], [entry-1, entry]]
/      - reduce by:
c       -   choose       -> [(row choose entry-1), (row-1 choose entry)]
6,1  - 6 paired with 1 = [6,1]
x     - times        i.e. [a, a, a, a, a, a, a, b]
S - sum            -> 6*(row choose entry-1) + (row-1 choose entry)
`