Difference between Sum of Cubes and Sum of First N Natural Numbers

Given an integer N, find the absolute difference between sum of the cubes of first N natural numbers and the sum of first N natural numbers.

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

Approach:

  1. The sum of the cube of first N natural numbers, using the formula:
     (N ( N + 1 ) / 2) ^ 2
  2. The sum of first N numbers, using the formula:
     (N ( N + 1 ) / 2)
  3. The absolute difference between both the sums is
     S(S-1)
    where
     S = (N ( N + 1 ) / 2)

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

210

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, 29AjayKumar, jit_t

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.