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

📅  最后修改于: 2021-05-28 03:39:14             🧑  作者: Mango

给定正整数N ,任务是找到前N个自然数的立方的平均值。
例子:

天真的方法:天真的方法是找到前N个自然数的立方和,然后将其除以N。
下面是上述方法的实现:

C
// C program for the above approach
#include 
 
// Function to find average of cubes
double 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 Code
int main()
{
    // Given number
    int n = 3;
 
    // Function Call
    printf("%lf", findAverageOfCube(n));
    return 0;
}


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find average of cubes
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 Code
int main()
{
    // Given Number
    int n = 3;
 
    // Function Call
    cout << findAverageOfCube(n);
}


Java
// Java program for the above approach
import java.util.*;
import java.io.*;
class GFG{
 
// Function to find average of cubes
static 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 Code
public static void main(String[] args)
{
    // Given Number
    int n = 3;
 
    // Function Call
    System.out.print(findAverageOfCube(n));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program for the above approach
 
# Function to find average of cubes
def 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 Code
if __name__ == '__main__':
 
    # Given Number
    n = 3
 
    # Function Call
    print(findAverageOfCube(n))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find average of cubes
static 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 Code
public static void Main()
{
    // Given Number
    int n = 3;
 
    // Function Call
    Console.Write(findAverageOfCube(n));
}
}
 
// This code is contributed by Nidhi_biet


Javascript


C
// C program for the above approach
#include 
 
// Function to find average of cubes
double 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 Code
int main()
{
    // Given number
    int n = 3;
 
    // Function Call
    printf("%lf", findAverageOfCube(n));
 
    return 0;
}


C++
// C++ program for the above approach
#include 
using namespace std;
 
// function to find an average of cubes
double findAverageofCube(double n)
{
    // Apply the formula n(n+1)^2/4
    int ans = (n * (n + 1) * (n + 1)) / 4;
    return ans;
}
 
// Driver Code
int main()
{
    // Given Number
    int n = 3;
 
    // Function Call
    cout << findAverageofCube(n);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// function to find an average of cubes
static double findAverageofCube(double n)
{
    // Apply the formula n(n+1)^2/4
    int ans = (int)((n * (n + 1) * (n + 1)) / 4);
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Number
    int n = 3;
 
    // Function Call
    System.out.print(findAverageofCube(n));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program for the above approach
 
# Function to find average of cubes
def findAverageOfCube (n):
 
    # Apply the formula n*(n+1)^2/4
    ans = (n * (n + 1) * (n + 1)) / 4
    return ans
 
# Driver code
if __name__ == '__main__':
 
    # Given number
    n = 3
 
    # Function call
    print(findAverageOfCube(n))
 
# This code is contributed by himanshu77


C#
// C# program for the above approach
using System;
class GFG{
 
// function to find an average of cubes
static double findAverageofCube(double n)
{
    // Apply the formula n(n+1)^2/4
    int ans = (int)((n * (n + 1) * (n + 1)) / 4);
    return ans;
}
 
// Driver Code
public static void Main()
{
    // Given Number
    int n = 3;
 
    // Function Call
    Console.Write(findAverageofCube(n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
12.000000

时间复杂度: O(N)
高效方法:

因此,前N个自然数的立方和的平均值为

\frac{N*(N+1)^{2}}{4}

下面是上述方法的实现:

C

// C program for the above approach
#include 
 
// Function to find average of cubes
double 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 Code
int main()
{
    // Given number
    int n = 3;
 
    // Function Call
    printf("%lf", findAverageOfCube(n));
 
    return 0;
}

C++

// C++ program for the above approach
#include 
using namespace std;
 
// function to find an average of cubes
double findAverageofCube(double n)
{
    // Apply the formula n(n+1)^2/4
    int ans = (n * (n + 1) * (n + 1)) / 4;
    return ans;
}
 
// Driver Code
int main()
{
    // Given Number
    int n = 3;
 
    // Function Call
    cout << findAverageofCube(n);
 
    return 0;
}

Java

// Java program for the above approach
class GFG{
 
// function to find an average of cubes
static double findAverageofCube(double n)
{
    // Apply the formula n(n+1)^2/4
    int ans = (int)((n * (n + 1) * (n + 1)) / 4);
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Number
    int n = 3;
 
    // Function Call
    System.out.print(findAverageofCube(n));
}
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python3 program for the above approach
 
# Function to find average of cubes
def findAverageOfCube (n):
 
    # Apply the formula n*(n+1)^2/4
    ans = (n * (n + 1) * (n + 1)) / 4
    return ans
 
# Driver code
if __name__ == '__main__':
 
    # Given number
    n = 3
 
    # Function call
    print(findAverageOfCube(n))
 
# This code is contributed by himanshu77

C#

// C# program for the above approach
using System;
class GFG{
 
// function to find an average of cubes
static double findAverageofCube(double n)
{
    // Apply the formula n(n+1)^2/4
    int ans = (int)((n * (n + 1) * (n + 1)) / 4);
    return ans;
}
 
// Driver Code
public static void Main()
{
    // Given Number
    int n = 3;
 
    // Function Call
    Console.Write(findAverageofCube(n));
}
}
 
// This code is contributed by Code_Mech

Java脚本


输出:
12.000000

时间复杂度: O(1)