# Mean of fourth powers of first N natural numbers

Given a positive integer N, the task is to find the average of the fourth powers of the first N natural numbers.

Examples:

Input: N = 3
Output: 32.6667
Explanation:
The sum of the fourth powers of the first N natural numbers = 14 + 24 + 34 = 1 + 16 + 81 = 98.
Therefore, the average = 98 / 3 = 32.6667.

Input: N = 5
Output: 12

Naive Approach: The simplest approach to solve the given problem is to find the sum of the fourth powers of first N natural numbers and print its value when divided by N.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to find the average of the// fourth power of first N natural numbersdouble findAverage(int N){    // Stores the sum of the fourth    // powers of first N natural numbers    double S = 0;     // Calculate the sum of fourth power    for (int i = 1; i <= N; i++) {        S += i * i * i * i;    }     // Return the average    return S / N;} // Driver Codeint main(){    int N = 3;    cout << findAverage(N);     return 0;}

## Java

 // Java program for the above approachclass GFG{ // Function to find the average of the// fourth power of first N natural numbersstatic double findAverage(int N){         // Stores the sum of the fourth    // powers of first N natural numbers    double S = 0;     // Calculate the sum of fourth power    for(int i = 1; i <= N; i++)    {        S += i * i * i * i;    }     // Return the average    return S / N;} // Driver codepublic static void main(String[] args){    int N = 3;     System.out.println(findAverage(N));}} // This code is contributed by abhinavjain194

## Python3

 # Python3 program for the above approach # Function to find the average of the# fourth power of first N natural numbersdef findAverage(N):     # Stores the sum of the fourth    # powers of first N natural numbers    S = 0     # Calculate the sum of fourth power    for i in range(1, N + 1):        S += i * i * i * i     # Return the average    return round(S / N, 4) # Driver Codeif __name__ == '__main__':         N = 3    print(findAverage(N)) # This code is contributed by mohit kumar 29

## C#

 // C# program for the above approachusing System; class GFG{ // Function to find the average of the// fourth power of first N natural numbersstatic double findAverage(int N){         // Stores the sum of the fourth    // powers of first N natural numbers    double S = 0;      // Calculate the sum of fourth power    for(int i = 1; i <= N; i++)    {        S += i * i * i * i;    }      // Return the average    return S / N;}     // Driver Codepublic static void Main(){    int N = 3;         Console.WriteLine(findAverage(N));}} // This code is contriobuted by sanjoy_62

## Javascript

 

Output:
32.6667

Time Complexity: O(N)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by finding the sum of the fourth powers of the first N natural numbers by the mathematical formula given below and then print its value when divided by N.

The mathematical formula is as follows:

=>

=>

=>

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to find the average of the// fourth power of first N natural numbersdouble findAverage(int N){    // Store the resultant average    // calculated using formula    double avg = ((6 * N * N * N * N)                  + (15 * N * N * N)                  + (10 * N * N) - 1)                 / 30.0;     // Return the average    return avg;} // Driver Codeint main(){    int N = 3;    cout << findAverage(N);     return 0;}

## Java

 // Java program for the above approachimport java.util.*; class GFG{     // Function to find the average of the// fourth power of first N natural numbersstatic double findAverage(int N){         // Store the resultant average    // calculated using formula    double avg = ((6 * N * N * N * N) +                 (15 * N * N * N) +                  (10 * N * N) - 1) / 30.0;     // Return the average    return avg;} // Driver Codepublic static void main(String args[]){    int N = 3;         System.out.print(findAverage(N));}} // This code is contributed by shivanisinghss2110

## Python3

 # Python program for the above approach # Function to find the average of the# fourth power of first N natural numbersdef findAverage(N):         # Store the resultant average    # calculated using formula    avg = ((6 * N * N * N * N) + (15 * N * N * N) + (10 * N * N) - 1) / 30         # Return the average    return avg N = 3print(round(findAverage(N),4))      # This code is contributed by avanitrachhadiya2155

## C#

 // C# program for the above approachusing System; class GFG{     // Function to find the average of the// fourth power of first N natural numbersstatic double findAverage(int N){         // Store the resultant average    // calculated using formula    double avg = ((6 * N * N * N * N) +                 (15 * N * N * N) +                  (10 * N * N) - 1) / 30.0;     // Return the average    return avg;} // Driver Codepublic static void Main(){    int N = 3;    Console.WriteLine(findAverage(N));}} // This code is contributed by ukasp

## Javascript

 

Output:
32.6667

Time Complexity: O(1)
Auxiliary Space: O(1)

Previous
Next