Dado un entero positivo, N . Encuentre la suma del primer N término de la serie-
1 2 , (1 2 +2 2 ), (1 2 +2 2 +3 2 ),….,hasta N términos
Ejemplos :
Entrada : N = 3
Salida : 20
Entrada : N = 1
Salida : 1
Enfoque : La secuencia se forma usando el siguiente patrón. Para cualquier valor N-
Dado 1^2, (1^2+2^2), (1^2+2^2+3^2),….,hasta N términos
S norte = norte * (N+1) 2 * (N+2) / 12
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to return sum of // N term of the series int findSum(int N){ return N*(N+1)*(N+1)*(N+2)/12; } // Driver Code int main() { int N = 3; cout << findSum(N); }
Java
// Java program to implement // the above approach class GFG { // Function to return sum of // N term of the series static int findSum(int N) { return N * (N + 1) * (N + 1) * (N + 2) / 12; } // Driver Code public static void main(String args[]) { int N = 3; System.out.print(findSum(N)); } } // This code is contributed by Saurabh Jaiswal
Python3
# Python 3 program for the above approach # Function to return sum of # N term of the series def findSum(N): return N*(N+1)*(N+1)*(N+2)//12 # Driver Code if __name__ == "__main__": # Value of N N = 3 print(findSum(N)) # This code is contributed by Abhishek Thakur.
C#
// C# program to implement // the above approach using System; class GFG { // Function to return sum of // N term of the series static int findSum(int N){ return N*(N+1)*(N+1)*(N+2)/12; } // Driver Code public static void Main() { int N = 3; Console.Write(findSum(N)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // Javascript program to implement // the above approach // Function to return sum of // N term of the series function findSum(N){ return N*(N+1)*(N+1)*(N+2)/12; } // Driver Code let N = 3; document.write(findSum(N)); // This code is contributed by Samim Hossain Mondal. </script>
Producción
20
Complejidad de Tiempo : O(1)
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por akashjha2671 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA