Dado un número entero N , la tarea es encontrar la suma de los primeros N términos de la serie:
(2*3*5), (3*5*7), (4*7*9),…
Ejemplos:
Entrada: N = 3
Salida: 387
S 3 = (2 * 3 * 5) + (3 * 5 * 7) + (4 * 7 * 9) = 30 + 105 + 252 = 387
Entrada: N = 5
Salida: 1740
Método: Sea T n el término N de la serie . La suma de la serie se puede encontrar fácilmente observando el término N de la serie:
T n = {n ésimo término de 2, 3, 4, …} * {n ésimo término de 3, 5, 7, …} * {n ésimo término de 5, 7, 9, …}
T n = (n + 1) * (2 * n + 1) * (2* n + 3)
T n = 4n 3 + 12n 2 + 11n + 3
La suma ( S n ) de los primeros n términos se puede encontrar mediante
S norte = Σ T norte S norte = Σ[4n 3 + 12n 2 + 11n + 3]
S norte = ( n / 2) * [2n 3 + 12n 2 + 25n + 21]
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find sum of the // first n terms of the given series #include <bits/stdc++.h> using namespace std; // Function to return the sum of the // first n terms of the given series int calSum(int n) { // As described in the approach return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2; } // Driver code int main() { int n = 3; cout << calSum(n); return 0; }
Java
// Java program to find sum of the // first n terms of the given series class GFG { // Function to return the sum of the // first n terms of the given series static int calSum(int n) { // As described in the approach return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2; } // Driver Code public static void main(String args[]) { int n = 3; System.out.println(calSum(n)); } }
Python
# C++ program to find sum of the # first n terms of the given series # Function to return the sum of the # first n terms of the given series def calSum(n): # As described in the approach return (n*(2 * n*n * n + 12 * n*n + 25 * n + 21))/2; # Driver Code n = 3 print(calSum(n))
C#
// C# program to find sum of the // first n terms of the given series using System; class GFG { // Function to return the sum of the // first n terms of the given series static int calSum(int n) { // As described in the approach return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2; } // Driver code static public void Main() { int n = 3; Console.WriteLine(calSum(n)); } }
PHP
<?php // PHP script to find sum of the // first n terms of the given series // Function to return the sum of the // first n terms of the given series function calculateSum($n) { // As described in the approach return ($n*(2*$n*$n*$n+12*$n*$n+25*$n+21))/2; } // Driver code $n = 3; echo calculateSum($n); ?>
Javascript
<script> // Javascript program to find sum of the // first n terms of the given series // Function to return the sum of the // first n terms of the given series function calSum( n) { // As described in the approach return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2; } // Driver Code let n = 3; document.write(calSum(n)); // This code is contributed by 29AjayKumar </script>
387
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por mohit kumar 29 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA