7
\$\begingroup\$

This question already has an answer here:

Definition

Define the nth term of the CRAU sequence as follows.

  1. Begin with the singleton array A = [n].

  2. Do the following n times:

    For each integer k in A, replace the entry k with k natural numbers, counting from 1 to k.

  3. Compute the sum of all integers in A.

For example, if n = 3, we start with the list [3].

We replace 3 with 1, 2, 3, yielding [1, 2, 3].

We now replace 1, 2, and 3 with 1, 1, 2 and 1, 2, 3 (resp.), yielding [1, 1, 2, 1, 2, 3].

Finally, we perform the same replacements as in the previous step for all six integers in the array, yielding [1, 1, 1, 2, 1, 1, 2, 1, 2, 3].

The sum of the resulting integers is 15, so this is the third CRAU number.

Task

Write a program of a function that, given a strictly positive integer n as input, computes the nth term of the CRAU sequence.

This is . May the shortest code in bytes win!

Test cases

 1 ->       1
 2 ->       4
 3 ->      15
 4 ->      56
 5 ->     210
 6 ->     792
 7 ->    3003
 8 ->   11440
 9 ->   43758
10 ->  167960
11 ->  646646
12 -> 2496144
13 -> 9657700

Related

\$\endgroup\$

marked as duplicate by xnor code-golf Jun 21 '16 at 7:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \$\begingroup\$ This seems a rather roundabout way to present a challenge to compute choose(2*n,n-1). \$\endgroup\$ – xnor Jun 21 '16 at 7:27
  • 2
    \$\begingroup\$ @xnor chat.stackexchange.com/transcript/message/30500547#30500547 \$\endgroup\$ – Dennis Jun 21 '16 at 7:29
  • 1
    \$\begingroup\$ I'm calling it a dupe of a previous (disguised) binomial challenge. Maybe you can instead have the output be the full list without summing? \$\endgroup\$ – xnor Jun 21 '16 at 7:33
  • 5
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! Unfortunately this challenge is too similar to Code Golf: Number of paths! to warrant a separate post. I recommend posting future challenges to the Sandbox where they can get meaningful feedback before being posted to the main site. ;-P \$\endgroup\$ – Digital Trauma Jun 21 '16 at 19:24
  • 3
    \$\begingroup\$ @DigitalTrauma Thank you. I'll keep that in mind. \$\endgroup\$ – Dennis Jun 21 '16 at 22:20
3
\$\begingroup\$

MATL, 5 bytes

EGqXn

Try it online, or verify all test cases!

Explanation:

E       #Double the input
 Gq     #Push "input - 1"
   Xn   #Calculate "nchoosek" on the two numbers
\$\endgroup\$
2
\$\begingroup\$

Python, 65 bytes

def f(k):
 n,p=2*k,1
 for i in range(1,k):p=p*n//i;n-=1
 return p

Explanation: It's a simplified version of n choose k. See this sequence.

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

Retina, 27 bytes

$
$.`$*
M!&+%`.+(?=1)|^.+
0

Input in unary, using 0 as the unary digit.

Try it online!

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

J, 5 bytes

<:!+:

Uses the binomial coefficient of (2n, n-1).

For 22 bytes, this is a possible solution based on using the process described in the challenge.

[:+/([:;<@(1+i.)"0)^:]

Usage

Note: Extra commands used to format output for multiple input.

   f =: <:!+:
   (,.f"0) >: i. 13
 1       1
 2       4
 3      15
 4      56
 5     210
 6     792
 7    3003
 8   11440
 9   43758
10  167960
11  646646
12 2496144
13 9657700

Explanation

<:!+:  Input: n
   +:  Double the value of n to get 2*n
<:     Decrement n to get n-1
  !    Calculate the binomial coefficient of (2*n, n-1) and return

[:+/([:;<@(1+i.)"0)^:]  Input: n
                     ]  Identify function, gets the value n
    (     ...     )^:   Repeat the following n times with an initial value [n]
          (    )"0        Means rank 0, or to operate on each atom in the list
             i.           Create a range from 0 to that value, exclusive
           1+             Add 1 to each to make the range from 1 to that value
        <@                Box the value
     [:;                  Combine the boxes and unbox them to make a list and return
[:+/                    Sum the values in the list after n iterations and return
\$\endgroup\$

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