Count, Replace, Add Up!

7

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

Dennis

Posted 2016-06-21T06:57:38.270

Reputation: 196 637

Question was closed 2016-06-21T07:32:46.353

Is there a limit on the maximum number the program can handle? i.e. is it acceptable if my program returns a stack overflow for 13? – Fatalize – 2016-06-21T07:07:58.270

As long as your algorithm works for arbitrarily large numbers, your implementation may error if the input is too big. – Dennis – 2016-06-21T07:13:08.753

2This seems a rather roundabout way to present a challenge to compute choose(2*n,n-1). – xnor – 2016-06-21T07:27:58.557

2

@xnor http://chat.stackexchange.com/transcript/message/30500547#30500547

– Dennis – 2016-06-21T07:29:30.030

1I'm calling it a dupe of a previous (disguised) binomial challenge. Maybe you can instead have the output be the full list without summing? – xnor – 2016-06-21T07:33:46.560

5

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

– Digital Trauma – 2016-06-21T19:24:33.107

3@DigitalTrauma Thank you. I'll keep that in mind. – Dennis – 2016-06-21T22:20:00.897

Answers

3

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

James

Posted 2016-06-21T06:57:38.270

Reputation: 54 537

2

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.

Andrew Epstein

Posted 2016-06-21T06:57:38.270

Reputation: 341

1

Retina, 27 bytes

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

Input in unary, using 0 as the unary digit.

Try it online!

Martin Ender

Posted 2016-06-21T06:57:38.270

Reputation: 184 808

1

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

miles

Posted 2016-06-21T06:57:38.270

Reputation: 15 654