Programa para encontrar la suma de la serie 1 + 2 + 2 + 3 + 3 + 3 + . . . + norte

Dado un entero positivo n y la tarea es encontrar la suma de la serie 1 + 2 + 2 + 3 + 3 + 3 + . . . + n. 
Ejemplos: 
 

Input : n = 5
Output : 55
   = 1 + 2 + 2 + 3 + 3 + 3 + 4 + 4 + 4 + 
     4 + 5 + 5 + 5 + 5 + 5.
   = 55

Input : n = 10
Output : 385

Método de adición: En el método de adición se suman todos los elementos uno por uno. 
A continuación se muestra la implementación de este enfoque. 
 

C++

// Program to find
// sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
#include <bits/stdc++.h>
using namespace std;
 
// Function that find
// sum of series.
int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= i; j++)
            sum = sum + i;
    return sum;
}
 
// Driver function
int main()
{
    int n = 10;
 
    // Function call
    cout << sumOfSeries(n);
    return 0;
}

Java

// Java Program to
// find sum of
// series
// 1 + 2 + 2 + 3 +
// . . . + n
public class GfG{
 
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
         
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
                sum = sum + i;
         
        return sum;
    }
     
    // Driver Code
    public static void main(String s[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
         
    }
}
 
// This code is contributed by Gitanjali

Python3

# Python3 Program to
# find sum of series
# 1 + 2 + 2 + 3 +
# . . . + n
import math
 
# Function that find
# sum of series.
def sumOfSeries( n):
    sum = 0
    for i in range(1, n+1):
        sum = sum + i * i
    return sum
 
# Driver method
n = 10
 
# Function call
print (sumOfSeries(n))
 
# This code is contributed by Gitanjali

C#

// C# Program to find sum of
// series 1 + 2 + 2 + 3 + . . . + n
using System;
 
public class GfG {
 
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
 
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
                sum = sum + i;
 
        return sum;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.Write(sumOfSeries(n));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// Program to find
// sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
 
// Function that find
// sum of series.
function sumOfSeries($n)
{
    $sum = 0;
    for ($i = 1; $i <= $n; $i++)
        for ($j = 1; $j <= $i; $j++)
            $sum = $sum + $i;
    return $sum;
}
 
// Driver Code
$n = 10;
 
// Function call
echo(sumOfSeries($n));
 
// This code is contributed by Ajit.
?>

Javascript

<script>
// Javascript Program to
// find sum of
// series
// 1 + 2 + 2 + 3 +
// . . . + n
 
    // Function that find
    // sum of series.
    function sumOfSeries( n) {
        let sum = 0;
 
        for (let i = 1; i <= n; i++)
            for (let j = 1; j <= i; j++)
                sum = sum + i;
 
        return sum;
    }
 
    // Driver Code
        let n = 10;
        document.write(sumOfSeries(n));
 
 
// This code contributed by Princi Singh
 
</script>

Producción: 

385

Complejidad temporal: O(n 2 )

Espacio auxiliar: O(1)
Método de multiplicación: En el método de multiplicación cada elemento se multiplica por sí mismo y luego se suma. 
 

   Input n = 10
   sum = 1 + 2 + 2 + 3 + 3 + 3 + 4 + . . . + 10
       = 1 + 2 * 2 + 3 * 3 + 4 * 4 + . . . + 10 * 10
       = 1 + 4 + 9 + 16 + . . . + 100
       = 385

C++

// Program to find
// sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
#include <bits/stdc++.h>
using namespace std;
 
// Function to find
// sum of series.
int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum = sum + i * i;
    return sum;
}
 
// Driver function.
int main()
{
    int n = 10;
 
    // Function call
    cout << sumOfSeries(n);
    return 0;
}

Java

// Java Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
public class GfG{
 
    // Function that find sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + i * i;
        return sum;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
         
    }
}
 
// This code is contributed by Gitanjali

Python3

# Python3 Program to
# find sum of series
# 1 + 2 + 2 + 3 +
# . . . + n
import math
 
# Function that find
# sum of series.
def sumOfSeries( n):
    sum = 0
    for i in range(1, n+1):
        sum = sum + i * i
    return sum
 
# Driver method
n = 10
# Function call
print (sumOfSeries(n))
 
# This code is contributed by Gitanjali.

C#

// C# Program to find sum of series
// 1 + 2 + 2 + 3 + . . . + n
using System;
 
class GfG {
 
    // Function that find sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + i * i;
        return sum;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.WriteLine(sumOfSeries(n));
         
    }
}
 
// This code is contributed by anuj_67.

PHP

<?php
// Program to find
// sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
 
// Function to find
// sum of series.
function sumOfSeries($n)
{
    $sum = 0;
    for ($i = 1; $i <= $n; $i++)
        $sum = $sum + $i * $i;
    return $sum;
}
 
// Driver Code
$n = 10;
 
// Function call
echo(sumOfSeries($n));
 
// This code is contributed by Ajit.
?>

Javascript

<script>
// javascript Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
 
    // Function that find sum of series.
    function sumOfSeries(n)
    {
        var sum = 0;
        for (let i = 1; i <= n; i++)
            sum = sum + i * i;
        return sum;
    }
 
    // Driver Code
    var n = 10;
    document.write(sumOfSeries(n));
 
// This code is contributed by Amit Katiyar
</script>

Producción: 
 

385

Complejidad de tiempo: O(n)

Espacio auxiliar: O(1)
Usando la fórmula: También usamos la fórmula para encontrar la suma de series. 
 

    Input n = 10;
   Sum of series = (n * (n + 1) * (2 * n + 1)) / 6
    put n = 10 in the above formula
    sum = (10 * (10 + 1) * (2 * 10 + 1)) / 6
        = (10 * 11 * 21) / 6
        = 385

C++

// C++ Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
#include <bits/stdc++.h>
using namespace std;
 
// Function to find
// sum of series.
int sumOfSeries(int n)
{
    return (n * (n + 1) * (2 * n + 1)) / 6;
}
 
// Driver function
int main()
{
    int n = 10;
 
    // Function call
    cout << sumOfSeries(n);
    return 0;
}

Java

// Java Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
public class GfG
{
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * (2 * n + 1)) / 6;
    }
     
    // Driver Code
    public static void main(String s[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
         
    }
}
 
// This code is contributed by 'Gitanjali'.

Python3

# Python3 Program to
# find sum of series
# 1 + 2 + 2 + 3 +
# . . . + n
import math
 
# Function that find
# sum of series.
def sumOfSeries( n):
    return ((n * (n + 1) * (2 * n + 1)) / 6)
 
# Driver method
n = 10
 
# Function call
print (sumOfSeries(n))
 
# This code is contributed by Gitanjali

C#

// C# Program to find sum of series
// 1 + 2 + 2 + 3 + . . . + n
using System;
 
public class GfG {
     
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * (2 * n + 1)) / 6;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.WriteLine(sumOfSeries(n));
    }
}
 
// This code is contributed by 'vt_m'.

PHP

<?php
// PHP Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
 
// Function to find
// sum of series.
function sumOfSeries($n)
{
    return ($n * ($n + 1) *
           (2 * $n + 1)) / 6;
}
 
// Driver Code
$n = 10;
 
// Function call
echo(sumOfSeries($n));
 
// This code is contributed by Ajit.
?>

Javascript

<script>
// javascript Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
 
// Function that find
// sum of series.
function sumOfSeries(n)
{
    return (n * (n + 1) * (2 * n + 1)) / 6;
}
 
// Driver Code
var n = 10;
document.write(sumOfSeries(n));
 
 
// This code is contributed by Amit Katiyar
</script>

Producción : 
 

385

Complejidad de tiempo: O(1)

Espacio auxiliar: O(1)
Consulte la suma de cuadrados de números naturales para obtener detalles de la fórmula anterior y más optimizaciones.
 

Publicación traducida automáticamente

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