📜  前 N 个自然数的四次方的平均值

📅  最后修改于: 2022-05-13 01:56:05.770000             🧑  作者: Mango

前 N 个自然数的四次方的平均值

给定一个正整数N ,任务是求前N个自然数的四次方的平均值。

例子:

朴素方法:解决给定问题的最简单方法是找到前 N 个自然数的四次方之和,并在除以N时打印其值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


C++
// C++ program for the above approach
 
#include 
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


输出:
32.6667

时间复杂度: O(N)
辅助空间: O(1)

有效方法:上述方法也可以通过以下给出的数学公式找到前N个自然数的四次方之和,然后在除以N时打印其值来优化。

数学公式如下:

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
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


输出:
32.6667

时间复杂度: O(1)
辅助空间: O(1)