Average of Cubes of first N natural numbers - GeeksforGeeks

08 Oct, 2021
Given a positive integer N, the task is to find the average of cubes of first N natural numbers. Examples: 
Input: N = 2 Output: 4.5 Explanation: For integer N = 2, We have ( 13 + 23 ) = 1 + 8 = 9 average = 9 / 2 that is 4.5Input: N = 3 Output: 12 Explanation: For N = 3, We have ( 13 + 23 + 23 + 23 + 33 + 23 ) = 27 + 8 + 1 = 36 average = 36 / 3 that is 12 
 
Naive Approach: The naive approach is to find the sum of cubes of first N natural numbers and divide it by N.Below is the implementation of above approach:
C
C++
Java
Python3
C#
Javascript
// C program for the above approach#include <stdio.h> // Function to find average of cubesdouble findAverageOfCube(int n){    // Store sum of cubes of    // numbers in the sum    double sum = 0;     // Calculate sum of cubes    int i;    for (i = 1; i <= n; i++) {        sum += i * i * i;    }     // Return average    return sum / n;} // Driver Codeint main(){    // Given number    int n = 3;     // Function Call    printf("%lf", findAverageOfCube(n));    return 0;}
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find average of cubesdouble findAverageOfCube(int n){    // Storing sum of cubes    // of numbers in sum    double sum = 0;     // Calculate sum of cubes    for (int i = 1; i <= n; i++) {        sum += i * i * i;    }     // Return average    return sum / n;} // Driver Codeint main(){    // Given Number    int n = 3;     // Function Call    cout << findAverageOfCube(n);}
// Java program for the above approachimport java.util.*;import java.io.*;class GFG{ // Function to find average of cubesstatic double findAverageOfCube(int n){    // Storing sum of cubes    // of numbers in sum    double sum = 0;     // Calculate sum of cubes    for (int i = 1; i <= n; i++)    {        sum += i * i * i;    }     // Return average    return sum / n;} // Driver Codepublic static void main(String[] args){    // Given Number    int n = 3;     // Function Call    System.out.print(findAverageOfCube(n));}} // This code is contributed by shivanisinghss2110
# Python3 program for the above approach # Function to find average of cubesdef findAverageOfCube(n):     # Storing sum of cubes    # of numbers in sum    sum = 0     # Calculate sum of cubes    for i in range(1, n + 1):        sum += i * i * i     # Return average    return round(sum / n, 6) # Driver Codeif __name__ == '__main__':     # Given Number    n = 3     # Function Call    print(findAverageOfCube(n)) # This code is contributed by mohit kumar 29
// C# program for the above approachusing System;class GFG{ // Function to find average of cubesstatic double findAverageOfCube(int n){    // Storing sum of cubes    // of numbers in sum    double sum = 0;     // Calculate sum of cubes    for (int i = 1; i <= n; i++)    {        sum += i * i * i;    }     // Return average    return sum / n;} // Driver Codepublic static void Main(){    // Given Number    int n = 3;     // Function Call    Console.Write(findAverageOfCube(n));}} // This code is contributed by Nidhi_biet
<script>// javascript program for the above approach // Function to find average of cubesfunction findAverageOfCube( n){     // Store sum of cubes of    // numbers in the sum    let sum = 0;     // Calculate sum of cubes    let i;    for (i = 1; i <= n; i++) {        sum += i * i * i;    }     // Return average    return sum / n;} // Driver Code     // Given number    let n = 3;     // Function Call    document.write(findAverageOfCube(n).toFixed(6)); // This code is contributed by todaysgaurav </script>
12.000000
 
Time complexity: O(N) Efficient Approach:  
We know that, Sum of cubes of first N Natural Numbers = 
 Average is given by: =>  =>  =>  =>  
Therefore, the average of the cube sum of first N natural numbers is given by  Below is the implementation of above approach:
C
C++
Java
Python3
C#
Javascript
// C program for the above approach#include <stdio.h> // Function to find average of cubesdouble findAverageOfCube(int n){    // Store sum of cubes of    // numbers in the sum    double sum = 0;     // Calculate sum of cubes    int i;    for (i = 1; i <= n; i++) {        sum += i * i * i;    }     // Return average    return sum / n;} // Driver Codeint main(){    // Given number    int n = 3;     // Function Call    printf("%lf", findAverageOfCube(n));     return 0;}
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // function to find an average of cubesdouble findAverageofCube(double n){    // Apply the formula n(n+1)^2/4    int ans = (n * (n + 1) * (n + 1)) / 4;    return ans;} // Driver Codeint main(){    // Given Number    int n = 3;     // Function Call    cout << findAverageofCube(n);     return 0;}
// Java program for the above approachclass GFG{ // function to find an average of cubesstatic double findAverageofCube(double n){    // Apply the formula n(n+1)^2/4    int ans = (int)((n * (n + 1) * (n + 1)) / 4);    return ans;} // Driver Codepublic static void main(String[] args){    // Given Number    int n = 3;     // Function Call    System.out.print(findAverageofCube(n));}} // This code is contributed by shivanisinghss2110
# Python3 program for the above approach # Function to find average of cubesdef findAverageOfCube (n):     # Apply the formula n*(n+1)^2/4    ans = (n * (n + 1) * (n + 1)) / 4    return ans # Driver codeif __name__ == '__main__':     # Given number    n = 3     # Function call    print(findAverageOfCube(n)) # This code is contributed by himanshu77
// C# program for the above approachusing System;class GFG{ // function to find an average of cubesstatic double findAverageofCube(double n){    // Apply the formula n(n+1)^2/4    int ans = (int)((n * (n + 1) * (n + 1)) / 4);    return ans;} // Driver Codepublic static void Main(){    // Given Number    int n = 3;     // Function Call    Console.Write(findAverageofCube(n));}} // This code is contributed by Code_Mech
<script>// javascript program for the above approach// function to find an average of cubesfunction findAverageofCube(n){    // Apply the formula n(n+1)^2/4    var ans = parseInt(((n * (n + 1) * (n + 1)) / 4));    return ans;} // Driver Code // Given Numbervar n = 3; // Function Calldocument.write(findAverageofCube(n)); // This code is contributed by Amit Katiyar</script>
12.000000
 
Time Complexity: O(1)
mohit kumar 29
shivanisinghss2110
nidhi_biet
Code_Mech
himanshu77
todaysgaurav
amit143katiyar
gulshankumarar231
maths-cube
Natural Numbers
Aptitude
Articles
C Language
C Programs
C++
C++ Programs
Competitive Programming
Java
Mathematical
Puzzles
Python
Mathematical
Java
Puzzles
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Puzzle | How much money did the man have before entering the bank?
Program to find the last two digits of x^y
Puzzle | Splitting a Cake with a Missing Piece in two equal portion
Order and Ranking Questions & Answers
10 Tips and Tricks to Crack Internships and Placements
Tree Traversals (Inorder, Preorder and Postorder)
SQL | Join (Inner, Left, Right and Full Joins)
find command in Linux with examples
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Mutex vs Semaphore