Media de cuartas potencias de primeros N números naturales

Dado un entero positivo N , la tarea es encontrar el promedio de las cuartas potencias de los primeros N números naturales .

Ejemplos:

Entrada: N = 3
Salida: 32,6667
Explicación:
La suma de las cuartas potencias de los primeros N números naturales = 1 4 + 2 4 + 3 4 = 1 + 16 + 81 = 98.
Por tanto, la media = 98 / 3 = 32,6667 .

Entrada: N = 5
Salida: 12

Enfoque ingenuo: el enfoque más simple para resolver el problema dado es encontrar la suma de las cuartas potencias de los primeros N números naturales e imprimir su valor cuando se divide por N.

A continuación se muestra la implementación del enfoque anterior:

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>
Producción: 

32.6667

 

Complejidad temporal: O(N)
Espacio auxiliar: O(1)

Enfoque eficiente: el enfoque anterior también se puede optimizar al encontrar la suma de las cuartas potencias de los primeros N números naturales mediante la fórmula matemática que se proporciona a continuación y luego imprimir su valor cuando se divide por N .

La fórmula matemática es la siguiente:

=> 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}

A continuación se muestra la implementación del enfoque anterior:

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>
Producción: 

32.6667

 

Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por sam_2200 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *