Encuentra la suma de N términos de la serie 3 ^ 3 – 2 ^ 3, 5 ^ 3 – 4 ^ 3, 7 ^ 3 – 6 ^ 3, …

Dado un entero positivo N , la tarea es encontrar la suma hasta el N- ésimo término de la serie:

 3 3 – 2 3 , 5 3 – 4 3 , 7 3 – 6 3 , …., hasta N términos

Ejemplos :

Entrada : N = 10
Salida : 4960

Entrada : N = 1
Salida : 19

 

Enfoque ingenuo

  • Inicialice dos variables int pares e impares. Impar con valor 3 e par con valor 2.
  • Ahora iterar el ciclo for n veces cada vez calculará el término actual y lo agregará a la suma.
  • En cada iteración aumente el valor par e impar con 2.
  • Devolver la suma resultante

C++

// C++ program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return sum of
// N term of the series
 
int findSum(int N)
{
    // Initialize the variable
    int Odd = 3;
    int Even = 2;
    int Sum = 0;
 
    // Run a loop for N number of times
    for (int i = 0; i < N; i++) {
 
        // Calculate the current term
        // and add it to the sum
        Sum += (pow(Odd, 3)
                - pow(Even, 3));
 
        // Increase the odd and
        // even with value 2
        Odd += 2;
        Even += 2;
    }
    return Sum;
}
 
// Driver Code
int main()
{
    int N = 10;
    cout << findSum(N);
}

Java

// JAVA program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
import java.util.*;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  public static int findSum(int N)
  {
 
    // Initialize the variable
    int Odd = 3;
    int Even = 2;
    int Sum = 0;
 
    // Run a loop for N number of times
    for (int i = 0; i < N; i++) {
 
      // Calculate the current term
      // and add it to the sum
      Sum += (Math.pow(Odd, 3) - Math.pow(Even, 3));
 
      // Increase the odd and
      // even with value 2
      Odd += 2;
      Even += 2;
    }
    return Sum;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 10;
    System.out.print(findSum(N));
  }
}
 
// This code is contributed by Taranpreet

Python3

# Python 3 program for the above approach
 
# Function to calculate the sum
# of first N term
def findSum(N):
    # Initialize the variable
    Odd = 3
    Even = 2
    Sum = 0
 
    # Run a loop for N number of times
    for i in range(N):
 
        # Calculate the current term
        # and add it to the sum
        Sum += (pow(Odd, 3) - pow(Even, 3))
 
        # Increase the odd and
        # even with value 2
        Odd += 2
        Even += 2
         
    return Sum
 
 
# Driver Code
if __name__ == "__main__":
 
    # Value of N
    N = 10
     
    # Function call to calculate
    # sum of the series
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.

C#

// C# program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
using System;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  public static int findSum(int N)
  {
 
    // Initialize the variable
    int Odd = 3;
    int Even = 2;
    int Sum = 0;
 
    // Run a loop for N number of times
    for (int i = 0; i < N; i++) {
 
      // Calculate the current term
      // and add it to the sum
      Sum += (int)(Math. Pow(Odd, 3) - Math.Pow(Even, 3));
 
      // Increase the odd and
      // even with value 2
      Odd += 2;
      Even += 2;
    }
    return Sum;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 10;
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript

<script>
 
// Javascript program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
// Function to return sum of
// N term of the series
function findSum(N)
{
 
    // Initialize the variable
    let Odd = 3;
    let Even = 2;
    let Sum = 0;
 
    // Run a loop for N number of times
    for (let i = 0; i < N; i++) {
 
        // Calculate the current term
        // and add it to the sum
        Sum += (Math.pow(Odd, 3)
            - Math.pow(Even, 3));
 
        // Increase the odd and
        // even with value 2
        Odd += 2;
        Even += 2;
    }
    return Sum;
}
 
// Driver Code
 
let N = 10;
document.write(findSum(N));
 
// This code is contributed by gfgking.
</script>
Producción

4960

Complejidad de Tiempo : O(N)
Espacio Auxiliar : O(1), ya que no se ha tomado ningún espacio extra.

Enfoque eficiente :

La secuencia se forma usando el siguiente patrón.

Para cualquier valor N, la forma generalizada de la sucesión dada es: 
 

S N = 4*N 3 + 9*N 2 + 6*N

 

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return sum of
// N term of the series
 
int findSum(int N)
{
    return 4 * pow(N, 3) + 9 * pow(N, 2) + 6 * N;
}
 
// Driver Code
int main()
{
    int N = 10;
    cout << findSum(N);
}

Java

// Java program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
import java.util.*;
 
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static int findSum(int N)
  {
    return (int) (4 * Math.pow(N, 3) + 9 * Math.pow(N, 2) + 6 * N);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 10;
    System.out.print(findSum(N));
  }
}
 
// This code is contributed by 29AjayKumar

Python3

# Python 3 program to find the sum of N terms of the
# series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
# Function to calculate the sum
# of first N term
def findSum(N):
    return 4 * pow(N, 3) + 9 * pow(N, 2) + 6 * N
 
 
# Driver Code
if __name__ == "__main__":
 
    # Value of N
    N = 10
     
    # Function call to calculate
    # sum of the series
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.

C#

// C# program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
using System;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static int findSum(int N)
  {
    return 4 * (int)Math.Pow(N, 3)
      + 9 * (int)Math.Pow(N, 2) + 6 * N;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 10;
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by ukasp.

Javascript

<script>
        // JavaScript code for the above approach
 
 
        // Function to return sum of
        // N term of the series
 
        function findSum(N) {
            return 4 * Math.pow(N, 3) + 9 * Math.pow(N, 2) + 6 * N;
        }
 
        // Driver Code
 
        let N = 10;
        document.write(findSum(N));
 
 
 
 
      // This code is contributed by Potta Lokesh
 
    </script>
Producción

4960

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

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *