Diferencia entre la suma de cubos y la suma de los primeros N números naturales

Dado un entero N, encuentre la diferencia absoluta entre la suma de los cubos de los primeros N números naturales y la suma de los primeros N números naturales.
 

Input: N = 3
Output: 30
Sum of first three numbers is 3 + 2 + 1 = 6
Sum of Cube of first three numbers is = 1 + 8 + 27 = 36
Absolute difference = 36 - 6 = 30

Input: N = 5
Output: 210

Acercarse: 
 

  1. La suma del cubo de los primeros N números naturales, usando la fórmula: 
    (N ( N + 1 ) / 2) ^ 2
     
  2. La suma de los primeros N números, usando la fórmula: 
    (N ( N + 1 ) / 2)
     
  3. La diferencia absoluta entre ambas sumas es 
    S(S-1)
    donde 
    S = (N ( N + 1 ) / 2)
     

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

C++

// C++ program to find the difference
// between the sum of the cubes of the
// first N natural numbers and
// the sum of the first N natural number
 
#include <bits/stdc++.h>
using namespace std;
 
int difference(int n)
{
 
    int S, res;
 
    // Sum of first n natural numbers
    S = (n * (n + 1)) / 2;
 
    // Find the required difference
    res = S * (S - 1);
 
    return res;
}
 
// Driver Code
int main()
{
    int n = 5;
    cout << difference(n);
 
    return 0;
}

Java

// Java program to find the difference
// between the sum of the cubes of the
// first N natural numbers and
// the sum of the first N natural number
 
class GFG
{
 
static int difference(int n)
{
 
    int S, res;
 
    // Sum of first n natural numbers
    S = (n * (n + 1)) / 2;
 
    // Find the required difference
    res = S * (S - 1);
 
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 5;
    System.out.print(difference(n));
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program to find the difference
# between the sum of the cubes of the
# first N natural numbers and
# the sum of the first N natural number
def difference(n) :
 
    # Sum of first n natural numbers
    S = (n * (n + 1)) // 2;
 
    # Find the required difference
    res = S * (S - 1);
 
    return res;
 
# Driver Code
if __name__ == "__main__" :
 
    n = 5;
    print(difference(n));
     
# This code is contributed by AnkitRai01

C#

// C# program to find the difference
// between the sum of the cubes of the
// first N natural numbers and
// the sum of the first N natural number
using System;
 
class GFG
{
static int difference(int n)
{
    int S, res;
 
    // Sum of first n natural numbers
    S = (n * (n + 1)) / 2;
 
    // Find the required difference
    res = S * (S - 1);
 
    return res;
}
 
// Driver Code
static public void Main ()
{
    int n = 5;
    Console.Write(difference(n));
}
}
 
// This code is contributed by ajit

Javascript

<script>
// JavaScript program to find the difference
// between the sum of the cubes of the
// first N natural numbers and
// the sum of the first N natural number
   
    function difference(n)
    {
   
        let S, res;
   
        // Sum of first n natural numbers
        S = Math.floor((n * (n + 1)) / 2);
   
        // Find the required difference
        res = S * (S - 1);
   
        return res;
    }
   
    // Driver Code
 
    let n = 5;
    document.write(difference(n));
  
//This code is contributed by Surbhi Tyagi
 
</script>
Producción: 

210

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Aggr 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 *