Suma de la serie 1, 3, 6, 10… (Números triangulares)

Dado n, no de elementos en la serie, encuentre la suma de la serie 1, 3, 6, 10….n. La serie representa principalmente números triangulares .
Ejemplos: 
 

Input: 2
Output: 4
Explanation: 1 + 3 = 4

Input: 4
Output: 20
Explanation: 1 + 3 + 6 + 10 = 20

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)
{
    let sum = 0;
    for (let i = 1; i <= n; i++)
       sum += i * ((i + 1) / 2);
    return sum;
}
 
// Driver code
let n = 4;
  document.write(seriesSum(n)) ;
 
// This code is contributed by aashish1995
 
</script>

Producción: 
 

20

Complejidad del tiempo : O(n)

Espacio Auxiliar: O(1) ya que usa variables constantes

Una solución eficiente es utilizar la fórmula directa n(n+1)(n+2)/6
 

Let g(i) be i-th triangular number.
g(1) = 1
g(2) = 3
g(3) = 6
g(n) = n(n+1)/2
Let f(n) be the sum of the triangular
numbers 1 through n.
f(n) = g(1) + g(2) + ... + g(n)

Then:
f(n) = n(n+1)(n+2)/6

¿Cómo podemos probar esto? Podemos demostrarlo por inducción. Es decir, probar dos cosas: 
 

  1. Es cierto para algunos n (n = 1, en este caso).
  2. Si es cierto para n, entonces es cierto para n+1.

Esto nos permite concluir que es cierto para todo n >= 1.
 

Now 1) is easy. We know that f(1) = g(1) 
= 1. So it's true for n = 1.

Now for 2). Suppose it's true for n. 
Consider f(n+1). We have:
f(n+1) = g(1) + g(2) + ... + g(n) + g(n+1) 
       = f(n) + g(n+1)

Using our assumption f(n) = n(n+1)(n+2)/6 
and g(n+1) = (n+1)(n+2)/2, we have:
f(n+1) = n(n+1)(n+2)/6 + (n+1)(n+2)/2
       = n(n+1)(n+2)/6 + 3(n+1)(n+2)/6
       = (n+1)(n+2)(n+3)/6
Therefore, f(n) = 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
    let n = 4;
    document.write(seriesSum(n));
 
// This code is contributed by todaysgaurav
 
</script>

Producción: 
 

20

Complejidad temporal: O(1)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Twinkl Bajaj 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 *