Dado un número N , la tarea es encontrar la suma de la siguiente serie hasta 3N términos.
1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+… hasta 3N términos
Ejemplos:
Entrada: N = 2
Salida: 17Entrada: N = 3
Salida: 56
Enfoque ingenuo:
Si observamos claramente, podemos dividirlo en una agrupación de 3 términos que tienen N no. de grupos
1 a 3 términos = 1^3 +1^2 +1 = 3
4 a 6 términos = 2^3+2^2+2 = 14
7 a 9 términos = 3^3+3^2+ 3 = 39
.
.
(3N-2) a 3N término = N^3+N^2+ N
Los pasos a continuación se pueden usar para resolver el problema:
- Para cada iterativo i , calcule (i^3+i^2+i) .
- Y agregue el valor calculado a la suma (inicialmente, la suma será 0 ).
- Devolver la suma final .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the sum of the // series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+... // till 3N terms #include <bits/stdc++.h> using namespace std; // Function to return the sum // upto 3Nth term of the series int seriesSum(int N) { // Initial value of the sum int sum = 0; // Loop to iterate from 1 to N for (int i = 1; i <= N; i++) { // Adding current calculated value // to sum sum += (pow(i, 3) + pow(i, 2) + i); } // Return the sum upto 3Nth term return sum; } // Driver Code int main() { // Get the value of N int N = 5; cout << seriesSum(N); return 0; }
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to return the sum // upto 3Nth term of the series static int seriesSum(int N) { // Initial value of the sum int sum = 0; // Loop to iterate from 1 to N for (int i = 1; i <= N; i++) { // Adding current calculated value // to sum sum += (Math.pow(i, 3) + Math.pow(i, 2) + i); } // Return the sum upto 3Nth term return sum; } // Driver Code public static void main (String[] args) { int N = 5; System.out.print(seriesSum(N)); } } // This code is contributed by hrithikgarg03188
Python3
# Python code for the above approach # Function to return the sum # upto 3Nth term of the series def seriesSum(N): # Initial value of the sum sum = 0; # Loop to iterate from 1 to N for i in range(1, N + 1): # Adding current calculated value # to sum sum += (i ** 3) + (i ** 2) + i; # Return the sum upto 3Nth term return sum; # Driver Code # Get the value of N N = 5; print(seriesSum(N)); # This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach using System; class GFG { // Function to return the sum // upto 3Nth term of the series static int seriesSum(int N) { // Initial value of the sum int sum = 0; // Loop to iterate from 1 to N for (int i = 1; i <= N; i++) { // Adding current calculated value // to sum sum += ((int)Math.Pow(i, 3) + (int)Math.Pow(i, 2) + i); } // Return the sum upto 3Nth term return sum; } // Driver Code public static void Main () { int N = 5; Console.Write(seriesSum(N)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for the above approach // Function to return the sum // upto 3Nth term of the series function seriesSum(N) { // Initial value of the sum let sum = 0; // Loop to iterate from 1 to N for (let i = 1; i <= N; i++) { // Adding current calculated value // to sum sum += (Math.pow(i, 3) + Math.pow(i, 2) + i); } // Return the sum upto 3Nth term return sum; } // Driver Code // Get the value of N let N = 5; document.write(seriesSum(N)); // This code is contributed by Potta Lokesh </script>
295
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.
Enfoque eficiente:
A partir de la serie dada, encuentre la fórmula para el 3.° término:
La serie dada
Esto se puede escribir como-
-(1)
Las tres ecuaciones anteriores están en AP, por lo tanto, se pueden escribir como:
= N*(N+1)*(3*N^2+7*N+8)/12
Entonces, la suma de la serie hasta el término 3N se puede generalizar como:
C++
// C++ program to find the sum of the // series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+... // till 3N terms #include <bits/stdc++.h> using namespace std; // Function to return the sum // upto 3Nth term of the series int seriesSum(int N) { return N * (N + 1) * (3 * pow(N, 2) + 7 * N + 8) / 12; } // Driver Code int main() { // Get the value of N int N = 5; cout << seriesSum(N); return 0; }
Java
// Java program to find the sum of the // series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+... // till 3N terms import java.util.*; public class GFG { // Function to return the sum // upto 3Nth term of the series static int seriesSum(int N) { return N * (N + 1) * (3 * (int)Math.pow(N, 2) + 7 * N + 8) / 12; } // Driver Code public static void main(String args[]) { // Get the value of N int N = 5; System.out.print(seriesSum(N)); } } // This code is contributed by Samim Hosdsain Mondal.
Python
# Pyhton program to find the sum of the # series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+... # till 3N terms import math # Function to return the sum # upto 3Nth term of the series def seriesSum(N): return math.floor(N * (N + 1) * (3 * pow(N, 2) + 7 * N + 8) / 12) # Driver Code # Get the value of N N = 5 print(seriesSum(N)) # This code is contributed by Samim Hossain Mondal
C#
// C# program to find the sum of the // series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+... // till 3N terms using System; class GFG { // Function to return the sum // upto 3Nth term of the series static int seriesSum(int N) { return N * (N + 1) * (3 * (int)Math.Pow(N, 2) + 7 * N + 8) / 12; } // Driver Code public static void Main() { // Get the value of N int N = 5; Console.Write(seriesSum(N)); } } // This code is contributed by Samim Hosdsain Mondal.
Javascript
<script> // JavaScript program to find the sum of the // series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+... // till 3N terms // Function to return the sum // upto 3Nth term of the series const seriesSum = (N) => parseInt(N * (N + 1) * (3 * Math.pow(N, 2) + 7 * N + 8) / 12) // Driver Code // Get the value of N let N = 5; document.write(seriesSum(N)); // This code is contributed by rakeshsahni </script>
295
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por athakur42u y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA