Suma de los primeros n números naturales – Part 5

Dado un entero positivo n . La tarea es encontrar la suma de la suma de los primeros n números naturales.
Ejemplos: 
 

Input : n = 3
Output : 10
Sum of first natural number: 1
Sum of first and second natural number: 1 + 2 = 3
Sum of first, second and third natural number = 1 + 2 + 3 = 6
Sum of sum of first three natural number = 1 + 3 + 6 = 10

Input : n = 2
Output : 4

Una solución simple es sumar números triangulares uno por uno. 
 

C++

/* CPP program to find sum
 series 1, 3, 6, 10, 15, 21...
and then find its sum*/
#include <iostream>
using namespace std;
 
// Function to find the sum of series
int seriesSum(int n)
{
    int sum = 0;
    for (int i=1; i<=n; i++)
       sum += i*(i+1)/2;
    return sum;
}
 
// Driver code
int main()
{
    int n = 4;
    cout << seriesSum(n);
    return 0;
}

Java

// Java program to find sum
// series 1, 3, 6, 10, 15, 21...
// and then find its sum*/
import java.io.*;
 
class GFG {
         
    // Function to find the sum of series
    static int seriesSum(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
        sum += i * (i + 1) / 2;
        return sum;
    }
 
    // Driver code
    public static void main (String[] args)
    {
        int n = 4;
        System.out.println(seriesSum(n));
         
    }
}
 
// This article is contributed by vt_m

Python3

# Python3 program to find sum
# series 1, 3, 6, 10, 15, 21...
# and then find its sum.
 
# Function to find the sum of series
def seriessum(n):
     
    sum = 0
    for i in range(1, n + 1):
        sum += i * (i + 1) / 2
    return sum
     
# Driver code
n = 4
print(seriessum(n))
 
# This code is Contributed by Azkia Anam.

C#

// C# program to find sum
// series 1, 3, 6, 10, 15, 21...
// and then find its sum*/
using System;
 
class GFG {
 
    // Function to find the sum of series
    static int seriesSum(int n)
    {
        int sum = 0;
         
        for (int i = 1; i <= n; i++)
            sum += i * (i + 1) / 2;
             
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 4;
         
        Console.WriteLine(seriesSum(n));
    }
}
 
// This article is contributed by vt_m.

PHP

<?php
// PHP program to find sum
// series 1, 3, 6, 10, 15, 21...
// and then find its sum
 
// Function to find
// the sum of series
function seriesSum($n)
{
    $sum = 0;
    for ($i = 1; $i <= $n; $i++)
        $sum += $i * ($i + 1) / 2;
    return $sum;
}
 
// Driver code
$n = 4;
echo(seriesSum($n));
 
// This code is contributed by Ajit.
?>

Javascript

<script>
 
// javascript program to find sum
// series 1, 3, 6, 10, 15, 21...
// and then find its sum*/
 
    // Function to find the sum of series
    function seriesSum(n) {
        var sum = 0;
        for (i = 1; i <= n; i++)
            sum += i * ((i + 1) / 2);
        return sum;
    }
 
    // Driver code
     
        var n = 4;
        document.write(seriesSum(n));
 
 
// This code contributed by Rajput-Ji
 
</script>

Producción: 

20

Complejidad del tiempo: O(n)
Una solución eficiente es usar la fórmula directa n(n+1)(n+2)/6
Matemáticamente, necesitamos encontrar, Σ ((i * (i + 1))/2), donde 1 <= i <= n 
Entonces, resolvamos esta suma, 
 

Sum = Σ ((i * (i + 1))/2), where 1 <= i <= n
    = (1/2) * Σ (i * (i + 1))
    = (1/2) * Σ (i2 + i)
    = (1/2) * (Σ i2 + Σ i)

We know Σ i2 = n * (n + 1) * (2*n + 1) / 6 and 
Σ i = n * ( n + 1) / 2.
Substituting the value, we get,
Sum = (1/2) * ((n * (n + 1) * (2*n + 1) / 6) + (n * ( n + 1) / 2))  
    = n * (n + 1)/2 [(2n + 1)/6 + 1/2]
    = n * (n + 1) * (n + 2) / 6

A continuación se muestra la implementación del enfoque anterior: 
 

C++

/* CPP program to find sum
 series 1, 3, 6, 10, 15, 21...
and then find its sum*/
#include <iostream>
using namespace std;
 
// Function to find the sum of series
int seriesSum(int n)
{
    return (n * (n + 1) * (n + 2)) / 6;
}
 
// Driver code
int main()
{
    int n = 4;
    cout << seriesSum(n);
    return 0;
}

Java

// java program to find sum
// series 1, 3, 6, 10, 15, 21...
// and then find its sum
import java.io.*;
 
class GFG
{
    // Function to find the sum of series
    static int seriesSum(int n)
    {
        return (n * (n + 1) * (n + 2)) / 6;
    }
 
   // Driver code
    public static void main (String[] args) {
         
        int n = 4;
        System.out.println( seriesSum(n));
         
    }
}
 
// This article is contributed by vt_m

Python3

# Python 3 program to find sum
# series 1, 3, 6, 10, 15, 21...
# and then find its sum*/
 
# Function to find the sum of series
def seriesSum(n):
 
    return int((n * (n + 1) * (n + 2)) / 6)
 
 
# Driver code
n = 4
print(seriesSum(n))
 
# This code is contributed by Smitha.

C#

// C# program to find sum
// series 1, 3, 6, 10, 15, 21...
// and then find its sum
using System;
 
class GFG {
     
    // Function to find the sum of series
    static int seriesSum(int n)
    {
        return (n * (n + 1) * (n + 2)) / 6;
    }
 
    // Driver code
    public static void Main()
    {
 
        int n = 4;
         
        Console.WriteLine(seriesSum(n));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to find sum
// series 1, 3, 6, 10, 15, 21...
// and then find its sum
 
// Function to find
// the sum of series
function seriesSum($n)
{
    return ($n * ($n + 1) *
           ($n + 2)) / 6;
}
 
// Driver code
$n = 4;
echo(seriesSum($n));
 
// This code is contributed by Ajit.
?>

Javascript

<script>
// javascript program to find sum
// series 1, 3, 6, 10, 15, 21...
// and then find its sum
 
// Function to find the sum of series
function seriesSum(n)
{
    return (n * (n + 1) * (n + 2)) / 6;
}
 
// Driver code
var n = 4;
document.write( seriesSum(n));
 
// This code is contributed by shikhasingrajput
</script>

Producción: 
 

20

Complejidad del tiempo : O(1)
 

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 *