Suma del producto de r y rth Coeficiente binomial (r * nCr)

Dado un entero positivo n . La tarea es encontrar la suma del producto de r y r th Coeficiente binomial. En otras palabras, encuentre: Σ (r * n C r ) , donde 0 <= r <= n.
Ejemplos: 
 

Input : n = 2
Output : 4
0.2C0 + 1.2C1 + 2.2C2
= 0*2 + 1*2 + 2*1
= 4

Input : n = 5
Output : 80

Método 1 (Fuerza bruta): la idea es iterar un ciclo i de 0 a n y evaluar i * n C i y agregar a la variable de suma.
A continuación se muestra la implementación de este enfoque: 
 

C++

// CPP Program to find sum of product of r and
// rth Binomial Coefficient i.e summation r * nCr
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
 
// Return the first n term of binomial coefficient.
void binomialCoeff(int n, int C[])
{
    C[0] = 1; // nC0 is 1
 
    for (int i = 1; i <= n; i++) {
 
        // Compute next row of pascal triangle
        // using the previous row
        for (int j = min(i, n); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
}
 
// Return summation of r * nCr
int summation(int n)
{
    int C[MAX];
    memset(C, 0, sizeof(C));
 
    // finding the first n term of binomial
    // coefficient
    binomialCoeff(n, C);
 
    // Iterate a loop to find the sum.
    int sum = 0;
    for (int i = 0; i <= n; i++)
        sum += (i * C[i]);   
 
    return sum;
}
 
// Driven Program
int main()
{
    int n = 2;
    cout << summation(n) << endl;
    return 0;
}

Java

// Java Program to find sum
// of product of r and rth
// Binomial Coefficient i.e
// summation r * nCr
class GFG
{
static int MAX = 100;
 
// Return the first n term
// of binomial coefficient.
static void binomialCoeff(int n,
                          int C[])
{
    C[0] = 1; // nC0 is 1
 
    for (int i = 1; i <= n; i++)
    {
 
        // Compute next row of
        // pascal triangle using
        // the previous row
        for (int j = Math.min(i, n); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
}
 
// Return summation
// of r * nCr
static int summation(int n)
{
    int C[] = new int[MAX];
     
    for(int i = 0; i < MAX; i++)
    C[i] = 0;
 
    // finding the first n term 
    // of binomial coefficient
    binomialCoeff(n, C);
 
    // Iterate a loop
    // to find the sum.
    int sum = 0;
    for (int i = 0; i <= n; i++)
        sum += (i * C[i]);
 
    return sum;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 2;
    System.out.println( summation(n));
}
}
 
// This code is contributed by Arnab Kundu

Python 3

# Python 3 Program to find sum of product
# of r and rth Binomial Coefficient i.e
# summation r * nCr
MAX = 100
 
# Return the first n term of
# binomial coefficient.
def binomialCoeff(n, C):
 
    C[0] = 1 # nC0 is 1
 
    for i in range(1, n + 1):
 
        # Compute next row of pascal triangle
        # using the previous row
        for j in range(min(i, n), -1, -1):
            C[j] = C[j] + C[j - 1]
 
# Return summation of r * nCr
def summation( n):
 
    C = [0] * MAX
 
    # finding the first n term of
    # binomial coefficient
    binomialCoeff(n, C)
 
    # Iterate a loop to find the sum.
    sum = 0
    for i in range(n + 1):
        sum += (i * C[i])
 
    return sum
 
# Driver Code
if __name__ == "__main__":
     
    n = 2
    print(summation(n))
 
# This code is contributed by ita_c

C#

// C# Program to find sum
// of product of r and rth
// Binomial Coefficient i.e
// summation r * nCr
using System;
 
class GFG
{
static int MAX = 100;
 
// Return the first n term
// of binomial coefficient.
static void binomialCoeff(int n,
                          int []C)
{
    C[0] = 1; // nC0 is 1
 
    for (int i = 1; i <= n; i++)
    {
 
        // Compute next row of
        // pascal triangle using
        // the previous row
        for (int j = Math.Min(i, n);
                 j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
}
 
// Return summation
// of r * nCr
static int summation(int n)
{
    int []C = new int[MAX];
     
    for(int i = 0; i < MAX; i++)
    C[i] = 0;
 
    // finding the first n term
    // of binomial coefficient
    binomialCoeff(n, C);
 
    // Iterate a loop
    // to find the sum.
    int sum = 0;
    for (int i = 0; i <= n; i++)
        sum += (i * C[i]);
 
    return sum;
}
 
// Driver Code
public static void Main()
{
    int n = 2;
    Console.Write( summation(n));
}
}
 
// This code is contributed
// by shiv_bhakt

PHP

<?php
// PHP Program to find sum of product
// of r and rth Binomial Coefficient
// i.e summation r * nCr
$MAX = 100;
 
// Return the first n term of
// binomial coefficient.
function binomialCoeff($n, &$C)
{
    $C[0] = 1; // nC0 is 1
 
    for ($i = 1; $i <= $n; $i++)
    {
 
        // Compute next row of pascal triangle
        // using the previous row
        for ($j = min($i, $n); $j > 0; $j--)
            $C[$j] = $C[$j] + $C[$j - 1];
    }
}
 
// Return summation of r * nCr
function summation($n)
{
    global $MAX;
    $C = array_fill(0, $MAX, 0);
 
    // finding the first n term of
    // binomial coefficient
    binomialCoeff($n, $C);
 
    // Iterate a loop to find the sum.
    $sum = 0;
    for ($i = 0; $i <= $n; $i++)
        $sum += ($i * $C[$i]);
 
    return $sum;
}
 
// Driver Code
$n = 2;
echo summation($n);
 
// This code is contributed by mits
?>

Javascript

<script>
 
    // Javascript Program to find sum of product of r and
    // rth Binomial Coefficient i.e summation r * nCr
    MAX = 100
 
    // Return the first n term of binomial coefficient.
    function binomialCoeff(n, C) {
      C[0] = 1; // nC0 is 1
 
      for (var i = 1; i <= n; i++) {
 
        // Compute next row of pascal triangle
        // using the previous row
        for (var j = Math.min(i, n); j > 0; j--)
          C[j] = C[j] + C[j - 1];
      }
    }
 
    // Return summation of r * nCr
    function summation(n) {
      C = Array(MAX).fill(0);
 
      // finding the first n term of binomial
      // coefficient
      binomialCoeff(n, C);
 
      // Iterate a loop to find the sum.
      var sum = 0;
      for (var i = 0; i <= n; i++)
        sum += (i * C[i]);
 
      return sum;
    }
 
    // Driven Program
    var n = 2;
    document.write(summation(n));
     
    // This code is contributed by noob2000.
  </script>
Producción: 

4

 

Método 2 (Usando la fórmula): 
Matemáticamente necesitamos encontrar, 
Σ (i * n C i ), donde 0 <= i <= n 
= Σ ( i C 1 * n C i ), (Dado que n C 1 = n, podemos escribir i como i C 1
= Σ ( (i! / (i – 1)! * 1!) * (n! / (n – i)! * i!) 
Al cancelar i! del numerador y denominador 
= Σ (n! / (i – 1)! * (n – i)!) 
= Σ n * ((n – 1)!/ (i – 1)! * (n – i)!) 
(Usando el reverso de n C r = (n)!/ (r)!* (n – r)!) 
= n * Σ n – 1 Cr – 1 
= n * 2 n – 1 (Ya que Σ n C r = 2 n )
A continuación se muestra la implementación de este enfoque: 
 

C++

// CPP Program to find sum of product of r and
// rth Binomial Coefficient i.e summation r * nCr
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
 
// Return summation of r * nCr
int summation(int n)
{
    return n << (n - 1);
}
 
// Driven Program
int main()
{
    int n = 2;
    cout << summation(n) << endl;
    return 0;
}

Java

// Java Program to find sum of product of
// r and rth Binomial Coefficient i.e
// summation r * nCr
import java.io.*;
 
class GFG {
 
    static int MAX = 100;
     
    // Return summation of r * nCr
    static int summation(int n)
    {
        return n << (n - 1);
    }
     
    // Driven Program
    public static void main (String[] args)
    {
        int n = 2;
        System.out.println( summation(n));
    }
}
 
// This code is contributed by anuj_67.

Python3

# Python3 Program to
# find sum of product
# of r and rth Binomial
# Coefficient i.e
# summation r * nCr
 
# Return summation
# of r * nCr
def summation( n):
    return n << (n - 1);
 
# Driver Code
n = 2;
print(summation(n));
 
# This code is contributed
# by mits

C#

// C# Program to find sum of product of
// r and rth Binomial Coefficient i.e
// summation r * nCr
using System;
 
class GFG {
 
    //static int MAX = 100;
     
    // Return summation of r * nCr
    static int summation(int n)
    {
        return n << (n - 1);
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 2;
        Console.WriteLine( summation(n));
    }
}
 
// This code is contributed by anuj_67.

PHP

<?php
// PHP Program to find sum
// of product of r and
// rth Binomial Coefficient
// i.e summation r * nCr
 
// Return summation of r * nCr
function summation( $n)
{
    return $n << ($n - 1);
}
 
// Driver Code
$n = 2;
echo summation($n) ;
 
// This code is contributed
// by shiv_bhakt
?>

Javascript

<script>
 
// Javascript Program to find sum of product of r and
// rth Binomial Coefficient i.e summation r * nCr
var MAX=100
 
// Return summation of r * nCr
function summation(n)
{
    return n << (n - 1);
}
 
// Driven Program
var n = 2;
document.write(summation(n));
     
</script>
Producción: 

4

 

Publicación traducida automáticamente

Artículo escrito por anuj0503 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *