Dado un número N, la tarea es encontrar la suma de los primeros N términos de la siguiente serie:
Sn = 2 + 6 + 12 + 20 + 30 … hasta n términos
Ejemplos:
Input: N = 2 Output: 8 Explanation: 2 + 6 = 8 Input: N = 4 Output: 40 Explanation: 2 + 6+ 12 + 20 = 40
Enfoque: Sea, el término n-ésimo sea denotado por Sn.
Este problema se puede resolver fácilmente dividiendo cada término de la siguiente manera:
Sn = 2 + 6 + 12 + 20 + 30...... Sn = (1+1^2) + (2+2^2) + (3+3^2) + (4+4^2) +...... Sn = (1 + 2 + 3 + 4 + ...unto n terms) + (1^2 + 2^2 + 3^2 + 4^2 + ...upto n terms)
Observamos que Sn puede descomponerse en la suma de dos series.
Por lo tanto, la suma de los primeros n términos se da de la siguiente manera:
Sn = (1 + 2 + 3 + 4 + ...unto n terms) + (1^2 + 2^2 + 3^2 + 4^2 + ...upto n terms) Sn = n*(n + 1)/2 + n*(n + 1)*(2*n + 1)/6
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find sum of first n terms #include <bits/stdc++.h> using namespace std; // Function to calculate the sum int calculateSum(int n) { return n * (n + 1) / 2 + n * (n + 1) * (2 * n + 1) / 6; } // Driver code int main() { // number of terms to be // included in the sum int n = 3; // find the Sn cout << "Sum = " << calculateSum(n); return 0; }
Java
// Java program to find sum of first n terms import java.util.*; import java.lang.*; import java.io.*; class GFG { // Function to calculate the sum static int calculateSum(int n) { return n * (n + 1) / 2 + n * (n + 1) * (2 * n + 1) / 6; } // Driver code public static void main(String args[]) { // number of terms to be // included in the sum int n = 3; // find the Sn System.out.print("Sum = " + calculateSum(n)); } }
Python3
# Python program to find sum of # first n terms # Function to calculate the sum def calculateSum(n): return (n * (n + 1) // 2 + n * (n + 1) * (2 * n + 1) // 6) # Driver code # number of terms to be # included in the sum n = 3 # find the Sum print("Sum = ", calculateSum(n)) # This code is contributed by # Sanjit_Prasad
C#
// C# program to find sum of // first n terms using System; class GFG { // Function to calculate the sum static int calculateSum(int n) { return n * (n + 1) / 2 + n * (n + 1) * (2 * n + 1) / 6; } // Driver code public static void Main() { // number of terms to be // included in the sum int n = 3; // find the Sn Console.WriteLine("Sum = " + calculateSum(n)); } } // This code is contributed by inder_verma
PHP
<?php // PHP program to find sum // of first n terms // Function to calculate the sum function calculateSum($n) { return $n * ($n + 1) / 2 + $n * ($n + 1) * (2 * $n + 1) / 6; } // Driver code // number of terms to be // included in the sum $n = 3; // find the Sn echo "Sum = " , calculateSum($n); // This code is contributed // by inder_verma ?>
Javascript
<script> // Javascript program to find sum of first n terms // Function to calculate the sum function calculateSum(n) { return n * (n + 1) / 2 + n * (n + 1) * (2 * n + 1) / 6; } // Driver code // number of terms to be // included in the sum let n = 3; // find the Sn document.write("Sum = " + calculateSum(n)); // This code is contributed by Mayank Tyagi </script>
Sum = 20
Complejidad de tiempo: O(1), estamos usando solo operaciones de tiempo constante.
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Shashank_Sharma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA