16
\$\begingroup\$

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

Task

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

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

18 Answers 18

7
\$\begingroup\$

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
answered Jul 6, 2017 at 14:25
\$\endgroup\$
7
\$\begingroup\$

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.

answered Jul 6, 2017 at 14:19
\$\endgroup\$
1
  • \$\begingroup\$ Pascal's triangle! \$\endgroup\$ Commented Jul 6, 2017 at 20:57
5
\$\begingroup\$

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.
answered Jul 6, 2017 at 15:00
\$\endgroup\$
3
  • \$\begingroup\$ 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= \$\endgroup\$ Commented Jul 6, 2017 at 15:08
  • \$\begingroup\$ _0\+ instead of 0X$+ is the same byte count if you prefer. \$\endgroup\$ Commented Jul 6, 2017 at 15:08
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Jul 6, 2017 at 15:11
4
\$\begingroup\$

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

answered Jul 6, 2017 at 14:37
\$\endgroup\$
3
\$\begingroup\$

Python 2, 67 bytes

a,b=input()
x=[1,6]
exec"x=map(sum,zip([0]+x,x+[6]));"*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

answered Jul 6, 2017 at 14:15
\$\endgroup\$
3
\$\begingroup\$

Python 3, (削除) 64 (削除ここまで) (削除) 60 (削除ここまで) 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!
answered Jul 6, 2017 at 14:38
\$\endgroup\$
4
  • 2
    \$\begingroup\$ 60 bytes \$\endgroup\$ Commented Jul 6, 2017 at 14:44
  • 2
    \$\begingroup\$ 52 bytes replacing the if/else with boolean operators and with a more flexible output \$\endgroup\$ Commented Jul 6, 2017 at 14:54
  • \$\begingroup\$ @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? \$\endgroup\$ Commented Jul 6, 2017 at 15:03
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ Commented Jul 6, 2017 at 15:20
3
\$\begingroup\$

Haskell, 41 bytes

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.

answered Jul 6, 2017 at 15:52
\$\endgroup\$
3
\$\begingroup\$

Husk, (削除) 15 (削除ここまで) 12 bytes

!!¡S`JẊ+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.

answered Oct 21, 2020 at 12:50
\$\endgroup\$
1
  • \$\begingroup\$ 12 bytes using J and recycling the d166... \$\endgroup\$ Commented Oct 22, 2020 at 12:41
2
\$\begingroup\$

Mathematica, 32 bytes

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

input

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

answered Jul 6, 2017 at 14:15
\$\endgroup\$
2
\$\begingroup\$

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!

answered Oct 22, 2020 at 13:02
\$\endgroup\$
2
\$\begingroup\$

Haskell, (削除) 40 (削除ここまで) 36 bytes

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.

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

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.

answered Oct 22, 2020 at 0:14
\$\endgroup\$
2
\$\begingroup\$

Desmos, (削除) 118 (削除ここまで) (削除) 71 (削除ここまで) 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.

answered Oct 24, 2020 at 21:28
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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)!} \$\endgroup\$ Commented Oct 24, 2020 at 23:23
  • \$\begingroup\$ @ovs Nice! I was struggling with getting the 1, 6, 6, and didn't think about 0^k \$\endgroup\$ Commented Oct 24, 2020 at 23:32
1
\$\begingroup\$

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!

answered Jul 6, 2017 at 15:15
\$\endgroup\$
1
  • \$\begingroup\$ 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(). \$\endgroup\$ Commented Oct 22, 2020 at 13:34
1
\$\begingroup\$

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.

answered Jul 6, 2017 at 15:50
\$\endgroup\$
1
\$\begingroup\$

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

answered Jul 6, 2017 at 16:55
\$\endgroup\$
1
\$\begingroup\$

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[1];$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[2]];

Try it online!

answered Jul 6, 2017 at 15:00
\$\endgroup\$
1
\$\begingroup\$

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)
answered Jul 6, 2017 at 20:54
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.