10
\$\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

\$\endgroup\$
  • 1
    \$\begingroup\$ Can we build a solution with zero in the first row? like in the OEIS sequence \$\endgroup\$ – Jörg Hülsermann Jul 6 '17 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\$ – Sriotchilism O'Zaic Jul 6 '17 at 14:43

13 Answers 13

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
\$\endgroup\$
6
\$\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.

\$\endgroup\$
  • \$\begingroup\$ Pascal's triangle! \$\endgroup\$ – Henry Jul 6 '17 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.
\$\endgroup\$
  • \$\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\$ – Martin Ender Jul 6 '17 at 15:08
  • \$\begingroup\$ _0\+ instead of 0X$+ is the same byte count if you prefer. \$\endgroup\$ – Martin Ender Jul 6 '17 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\$ – Business Cat Jul 6 '17 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

\$\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

\$\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!
\$\endgroup\$
  • 2
    \$\begingroup\$ 60 bytes \$\endgroup\$ – totallyhuman Jul 6 '17 at 14:44
  • 2
    \$\begingroup\$ 52 bytes replacing the if/else with boolean operators and with a more flexible output \$\endgroup\$ – Rod Jul 6 '17 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\$ – Chase Vogeli Jul 6 '17 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\$ – Uriel Jul 6 '17 at 15:20
  • \$\begingroup\$ @Uriel thank you for the clarification. \$\endgroup\$ – Chase Vogeli Jul 6 '17 at 15:32
2
\$\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.

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 32 bytes

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

input

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

\$\endgroup\$
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.

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

\$\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!

\$\endgroup\$
0
\$\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!

\$\endgroup\$
0
\$\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)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.