Programa para imprimir la suma del enésimo término dado

Dado el valor de n. Tienes que encontrar la suma de la serie donde el término n de la secuencia está dado por:
T n = n 2 – ( n – 1 ) 2
Ejemplos : 

Input : 3
Output : 9

Explanation: So here the term of the sequence upto n = 3 are: 
             1, 3, 5 And hence the required sum is = 1 + 3 + 5 = 9

Input : 6
Output : 36

Enfoque simple 
Simplemente use un ciclo y calcule la suma de cada término e imprima la suma. 

C++

// CPP program to find summation of series
#include <bits/stdc++.h>
using namespace std;
 
int summingSeries(long n)
{
    // use of loop to calculate
    // sum of each term
    int S = 0;
    for (int i = 1; i <= n; i++)
        S += i * i - (i - 1) * (i - 1);
     
    return S;
}
 
// Driver Code
int main()
{
    int n = 100;
    cout << "The sum of n term is: "
        << summingSeries(n) << endl;
    return 0;
}

Java

// JAVA program to find summation of series
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
 
class GFG
{
 
    // function to calculate sum of series
    static int summingSeries(long n)
    {
        // use of loop to calculate
        // sum of each term
        int S = 0;
        for (i = 1; i <= n; i++)
            S += i * i - (i - 1) * (i - 1);    
         
        return S;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 100;
        System.out.println("The sum of n term is: " +
                            summingSeries(n));
    }
}

Python3

# Python3 program to find summation
# of series
 
def summingSeries(n):
 
    # use of loop to calculate
    # sum of each term
    S = 0
    for i in range(1, n+1):
        S += i * i - (i - 1) * (i - 1)
     
    return S
 
# Driver Code
n = 100
print("The sum of n term is: ",
           summingSeries(n), sep = "")
# This code is contributed by Smitha.

C#

// C# program to illustrate...
// Summation of series
using System;
 
class GFG
{
 
    // function to calculate sum of series
    static int summingSeries(long n)
    {
 
        // Using the pow function calculate
        // the sum of the series
        return (int)Math.Pow(n, 2);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 100;
        Console.Write("The sum of n term is: " +
                        summingSeries(n));
    }
}
 
// This code contribute by Parashar...

PHP

<?php
// PHP program to find
// summation of series
 
function summingSeries( $n)
{
     
    // use of loop to calculate
    // sum of each term
    $S = 0;
    for ($i = 1; $i <= $n; $i++)
         $S += $i * $i - ($i - 1) *
                       ($i - 1);
     
    return $S;
}
 
// Driver Code
$n = 100;
echo "The sum of n term is: ",
summingSeries($n) ;
 
// This code contribute by vt_m.
?>

Javascript

<script>
// Javascript program to find summation of series
 
function summingSeries(n)
{
    // use of loop to calculate
    // sum of each term
    let S = 0;
    for (let i = 1; i <= n; i++)
        S += i * i - (i - 1) * (i - 1);
     
    return S;
}
 
// Driver Code
let n = 100;
document.write("The sum of n term is: " + summingSeries(n));
 
// This code is contributed by rishavmahato348.
</script>

Producción: 

The sum of n term is: 10000

Complejidad de tiempo – O(N) 
Complejidad de espacio – O(1)
Enfoque eficiente 
El uso del enfoque matemático puede resolver este problema de manera más eficiente.
 

T n = n 2 – (n-1) 2
La suma de la serie viene dada por (S) = SUM( T n )
TOMEMOS UN EJEMPLO SI 
N = 4 
Significa que debe haber 4 términos en la serie por lo que
1 er término = 1 2 – ( 1 – 1 ) 2 
2 do término = 2 2 – ( 2 – 1 ) 2 
3 o término = 3 2 – ( 3 – 1 ) 2 
4 o término = 4 2 – ( 3 – 1 ) 2
ASÍ QUE LA SUMA ESTÁ DADA POR = (1 – 0) + (4 – 1) + (9 – 4) + (16 – 9) 
= 16
DE ESTO TENEMOS AVISO DE QUE 1, 4, 9 SE CANCELAN DE LA SERIE 
SOLO QUEDA 16 QUE ES IGUAL AL ​​CUADRADO DE N
Entonces, de la serie anterior notamos que cada término se cancela del siguiente término, solo el último término es izquierda que es igual a N 2 .
 

C++

// CPP program to illustrate...
// Summation of series
 
#include <bits/stdc++.h>
using namespace std;
 
int summingSeries(long n)
{
    // Sum of n terms is n^2
    return pow(n, 2);
}
 
// Driver Code
int main()
{
    int n = 100;
    cout << "The sum of n term is: "
         << summingSeries(n) << endl;
    return 0;
}

Java

// JAVA program to illustrate...
// Summation of series
 
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
 
class GFG
{
 
    // function to calculate sum of series
    static int summingSeries(long n)
    {
 
        // Using the pow function calculate
        // the sum of the series
        return (int)Math.pow(n, 2);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 100;
        System.out.println("The sum of n term is: " +
                            summingSeries(n));
    }
}

Python3

# Python3 program to illustrate...
# Summation of series
import math
 
def summingSeries(n):
 
    # Sum of n terms is  n^2
    return math.pow(n, 2)
 
# Driver Code
n = 100
print ("The sum of n term is: ",
        summingSeries(n))
# This code is contributed by mits.

C#

// C# program to illustrate...
// Summation of series
using System;
 
class GFG
{
    // function to calculate sum of series
    static int summingSeries(long n)
    {
        // Using the pow function calculate
        // the sum of the series
        return (int)Math.Pow(n, 2);
    }
 
    // Driver code
    public static void Main()
    {
        int n = 100;
           Console.Write("The sum of n term is: " +
                              summingSeries(n));
    }
}
 
// This code is contributed by nitin mittal.

PHP

<?php
// PHP program to illustrate...
// Summation of series
 
function summingSeries($n)
{
    // Sum of n terms is  n^2
    return pow($n, 2);
}
 
// Driver Code
$n = 100;
echo "The sum of n term is: ",
summingSeries($n);
 
// This code contribute by vt_m.
?>

Javascript

<script>
// Javascript program to illustrate...
// Summation of series
 
function summingSeries(n)
{
    // Sum of n terms is n^2
    return Math.pow(n, 2);
}
 
// Driver Code
let n = 100;
document.write("The sum of n term is: "
    + summingSeries(n) + "<br>");
     
    // This code is contributed by subham348.
</script>

Producción: 

The sum of n term is: 10000

Complejidad temporal – O(1) 
Complejidad espacial – O(1) 
 

Publicación traducida automáticamente

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