12
\$\begingroup\$

Challenge:

Given inputs \$i\$ and \$n\$, calculate \$R_i(n)\$ where:

$$R_0(n)=n \\ R_i(n)=\sum_{j=0}^nR_{i-1}(j)$$

Note that \$R_1\$ is triangular function, and \$R_2\$ is tetrahedral function.

This is code golf, shortest answer in bytes wins.

Test cases:

[0,0]   => 0
[0,1]   => 1
[0,999] => 999
[1,0]   => 0
[1,1]   => 1
[1,2]   => 3
[1,3]   => 6
[1,4]   => 10
[1,10]  => 55
[1,30]  => 465
[1,99]  => 4950
[2,0]   => 0
[2,1]   => 1
[2,2]   => 4
[2,3]   => 10
[2,10]  => 220
[3,10]  => 715
[4,10]  => 2002
[4,11]  => 3003
[9,10]  => 92378
[32,6]  => 501942
\$\endgroup\$
6
  • 7
    \$\begingroup\$ For those that don't already know, you're looking for the 𝑛th number along the 𝑖th diagonal after the initial all-ones diagonal of Pascal's triangle. \$\endgroup\$ Commented Sep 24 at 16:06
  • 1
    \$\begingroup\$ Please add a plain English description of the challenge and a worked example. \$\endgroup\$ Commented Sep 25 at 8:37
  • 1
    \$\begingroup\$ @Rosario yes, you are correct. Also, R_i(0)=0 for almost any i. \$\endgroup\$ Commented Sep 25 at 11:06
  • 1
    \$\begingroup\$ I think there's a case for keeping one of them open, but a virtually identical challenge codegolf.stackexchange.com/q/219301 was dupe-hammered in the past \$\endgroup\$ Commented Sep 25 at 14:19
  • \$\begingroup\$ @att I think the older challenge should be reopened because it has more answers. I will not vote to reopen anyone, as that is up to the community to decide. \$\endgroup\$ Commented Sep 25 at 16:44

12 Answers 12

8
\$\begingroup\$

R, 21 bytes

\(i,n)choose(n+i,i+1)

Attempt This Online!

\$\endgroup\$
5
\$\begingroup\$

JavaScript (ES6), 32 bytes

-6 thanks to jdt
-1 thanks to doubleunary

f=(i,n)=>i*n?f(i-1,n)+f(i,n-1):n

Try it online!

\$\endgroup\$
0
4
\$\begingroup\$

Uiua, 9 bytes

-3 thanks to fmbalbuena!

⍣⊣0⍥\+⊙⇡₁

pad

⊙⇡₁  # get range from 1 to n
⍥\+  # get prefix sums i times
⍣⊣0  # take last (defaulting to zero)
\$\endgroup\$
2
  • \$\begingroup\$ there's a language bug doing something weird here so this answer has some odd behaviors right now? \$\endgroup\$ Commented Sep 24 at 14:57
  • 1
    \$\begingroup\$ I think you can golf by using range and repeating cumulative sum n times, then getting the last element, right? \$\endgroup\$ Commented Sep 24 at 15:01
4
\$\begingroup\$

Dyalog APL, 6 bytes

+!⍨1+⊣

Explanation: \$\binom{n + i}{i + 1}\$

\$\endgroup\$
4
\$\begingroup\$

Vyxal, 4 bytes

₌+›ƈ

Try it Online!

Boring nCr answer. Takes n then i.

Here's a more funny 2 byter:

Vyxal RG, 2 bytes

Try it Online!

Takes i then n

Explained

₌+›ƈ­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁡‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌­
₌+    # ‎⁡(i + n)
   ƈ  # ‎⁢choose
₌ ›   # ‎⁣(i + 1)
💎

Created with the help of Luminespire.

or

(¦­⁡​‎‎⁡⁠⁡‏⁠⁠‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‏​⁢⁠⁡‌⁤​‎‏​⁢⁠⁡‌­
(   # ‎⁡i times:
 ¦  # ‎⁢  Get the cumulative sum of the top of the stack, initially n.
# ‎⁣The R flag makes sure that the initial cumulative sum casts n to range(1, n) instead of summing the digits of n
# ‎⁤The G flag outputs the greatest value of the top of the stack, defaulting to the top of the stack if it's still a number (i.e. i = 0)
💎

Created with the help of Luminespire.

\$\endgroup\$
1
  • \$\begingroup\$ I was doing this ⟨10|2⟩∑2›ℂ but couldn't figure out how to make it into a function. Do you mind posting a solution with this variation, if possible? \$\endgroup\$ Commented Sep 25 at 3:45
3
\$\begingroup\$

C (gcc), 35 bytes

f(i,n){i=i&&n?f(i-1,n)+f(i,n-1):n;}

Try it online!

Simple port of Arnaulds answer.

\$\endgroup\$
2
\$\begingroup\$

Python, 44 43 41* bytes

lambda i,n:math.comb(n+i,i+1);import math

Attempt This Online!

Thanks to Toby Speight for saving 1 byte and Albert.Lang for saving 2 additional bytes.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ As your function is not recursive you needn't give it a name. In practice, that means that you can save 2 bytes by swapping the two lines and moving the name to the header (S=\) \$\endgroup\$ Commented Sep 24 at 16:46
2
\$\begingroup\$

Arturo, 35 bytes

f:$[i n][?0<i*n->+f i n-1f i-1n->n]

Try it!

Port of Arnauld's JavaScript answer.

\$\endgroup\$
2
\$\begingroup\$

Google Sheets, 28 bytes

=if(i*n,f(i-1,n)+f(i,n-1),n)

A named function that expects that its name is f and its arguments are i and n.

To use this as an anonymous function, add a lambda() wrapper (45 bytes):

lambda(f,i,n,if(i*n,f(f,i-1,n)+f(f,i,n-1),n))

To use the function in a formula without defining a named function, add let() (67 bytes when whitespace and n() removed):

=let(
  f, lambda(f, i, n,
    if(i * n,
       f(f, i - 1, n) + f(f, i, n - 1),
       n 
    )
  ),
  n(f(f, A1, B1))
)

screenshot

Uses Arnauld's pattern. Thanks to jdt for -5 bytes. Google Sheets limits recursion depth to 9999, and there are other computational limits as well, so the last two test cases [9,10] and [32,6] will error out.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ I understand that the consensus regarding functions is that they should include the f= in the byte count when the function calls itself. The 36-byte function doesn't do that, but my view is that the consensus is hard to apply to named functions because they're defined through a UI. What you enter in the UI is f, i, n so that's 3 bytes, but that discounts all the clicks to get into the right fields etc, so the whole thing seems moot. \$\endgroup\$ Commented Sep 24 at 17:01
  • \$\begingroup\$ The anonymous lambda() version calls itself by specifying its own name as the first argument in each call, and the let() version is fully stand-alone, so this discussion doesn't apply to them. \$\endgroup\$ Commented Sep 24 at 17:01
  • \$\begingroup\$ @jdt thanks, applied. \$\endgroup\$ Commented Sep 25 at 6:40
1
\$\begingroup\$

Charcoal, 15 bytes

≔…·⁰NθI÷Π⁺θNΠ⊕θ

Try it online! Link is to verbose version of code. Explanation:

≔…·⁰Nθ

Input \$ i \$ and generate a range from \$ 0 \$ to \$ i \$ inclusive.

I÷Π⁺θNΠ⊕θ

Input \$ n \$ and calculate \$ \binom{i + n}{i + 1} = \frac{(i + n)!}{(n - 1)!(i + 1)!} = \frac{n \times (1 + n) \times \dots \times (i + n)}{1 \times 2 \times \dots \times (i + 1)} \$ .

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

05AB1E, 4 bytes

+I>c

Inputs in the order \$n,i\$.

Try it online or verify all test cases.

Explanation:

\$\binom{i+n}{i+1}\$

+     # Add the two (implicit) inputs together: n+i
 I    # Push the second input-integer again: i
  >   # Increase it by 1: i+1
   c  # Choose/nCr builtin: (n+i) choose (i+1)
      # (which is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

APL(NARS), 21 chars

{0=⍺:⍵⋄+/(⍺-1)∇¨0,⍳⍵}

Input left [⍺] the name of function as not negative integer, in the right the value [⍵] as not negative integer. Output one not negative integer.

Recursive function as the definition (test problem not okay only for 32 h 6) the problem I think is due to the recalculation of the function_0..n-1 result without store it.

Note that if ⍵=0, ⍳⍵ is ⍬ (the void set of numeric in APL) and 0,⍳⍵ is one array of 1 element: the 0. As this below show

  ⍬ 
┌0─┐
│ 0│
└~─┘
  0,⍳0
┌1─┐
│ 0│
└~─┘

test:

  h←{0=⍺:⍵⋄+/(⍺-1)∇¨0,⍳⍵}
  0 h 0
0
  99 h 0
0
  0 h 99
99
  9 h 10
92378
  2 h 0
0
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.