Open In App

Mean of fourth powers of first N natural numbers

Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
using namespace std;
 
// Function to find the average of the
// fourth power of first N natural numbers
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 Code
int main()
{
    int N = 3;
    cout << findAverage(N);
 
    return 0;
}

                    

Java

// Java program for the above approach
class GFG{
 
// Function to find the average of the
// fourth power of first N natural numbers
static 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 code
public 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 numbers
def 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 Code
if __name__ == '__main__':
     
    N = 3
    print(findAverage(N))
 
# This code is contributed by mohit kumar 29

                    

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the average of the
// fourth power of first N natural numbers
static 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 Code
public static void Main()
{
    int N = 3;
     
    Console.WriteLine(findAverage(N));
}
}
 
// This code is contriobuted by sanjoy_62

                    

Javascript

<script>
 
// javascript program for the above approach
 
// Function to find the average of the
// fourth power of first N natural numbers
function findAverage(N)
{
    // Stores the sum of the fourth
    // powers of first N natural numbers
    var S = 0;
     
    var i;
    // Calculate the sum of fourth power
    for (i = 1; i <= N; i++) {
        S += i * i * i * i;
    }
 
    // Return the average
    return S / N;
}
 
// Driver Code
    var N = 3;
    document.write(findAverage(N));
 
</script>

                    

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:

=> Sum = \frac{(6N^5 + 15N^4 + 10N^3 - N)}{30}

=> Average = \frac{(6N^5 + 15N^4 + 10N^3 - N)}{30*N}

=> Average = \frac{(6N^4 + 15N^3 + 10N^2 - 1)}{30}

Below is the implementation of the above approach:

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the average of the
// fourth power of first N natural numbers
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 Code
int main()
{
    int N = 3;
    cout << findAverage(N);
 
    return 0;
}

                    

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the average of the
// fourth power of first N natural numbers
static 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 Code
public 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 numbers
def 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 = 3
print(round(findAverage(N),4))
     
 
# This code is contributed by avanitrachhadiya2155

                    

C#

// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the average of the
// fourth power of first N natural numbers
static 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 Code
public static void Main()
{
    int N = 3;
    Console.WriteLine(findAverage(N));
}
}
 
// This code is contributed by ukasp

                    

Javascript

<script>
 
    // JavaScript program for the above approach
 
// Function to find the average of the
// fourth power of first N natural numbers
function findAverage( N)
{
    // Store the resultant average
    // calculated using formula
    let avg = ((6 * N * N * N * N)
                + (15 * N * N * N)
                + (10 * N * N) - 1)
                / 30.0;
 
    // Return the average
    return avg;
}
 
// Driver Code
 
    let N = 3;
    document.write( findAverage(N).toFixed(4));
 
 
// This code is contributed by G.Sravan Kumar (171FA07058)
 
</script>

                    

Output: 
32.6667

 

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



Last Updated : 06 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads