Mean of fourth powers of first N natural numbers
Last Updated :
06 Oct, 2021
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++
#include <bits/stdc++.h>
using namespace std;
double findAverage( int N)
{
double S = 0;
for ( int i = 1; i <= N; i++) {
S += i * i * i * i;
}
return S / N;
}
int main()
{
int N = 3;
cout << findAverage(N);
return 0;
}
|
Java
class GFG{
static double findAverage( int N)
{
double S = 0 ;
for ( int i = 1 ; i <= N; i++)
{
S += i * i * i * i;
}
return S / N;
}
public static void main(String[] args)
{
int N = 3 ;
System.out.println(findAverage(N));
}
}
|
Python3
def findAverage(N):
S = 0
for i in range ( 1 , N + 1 ):
S + = i * i * i * i
return round (S / N, 4 )
if __name__ = = '__main__' :
N = 3
print (findAverage(N))
|
C#
using System;
class GFG{
static double findAverage( int N)
{
double S = 0;
for ( int i = 1; i <= N; i++)
{
S += i * i * i * i;
}
return S / N;
}
public static void Main()
{
int N = 3;
Console.WriteLine(findAverage(N));
}
}
|
Javascript
<script>
function findAverage(N)
{
var S = 0;
var i;
for (i = 1; i <= N; i++) {
S += i * i * i * i;
}
return S / N;
}
var N = 3;
document.write(findAverage(N));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
double findAverage( int N)
{
double avg = ((6 * N * N * N * N)
+ (15 * N * N * N)
+ (10 * N * N) - 1)
/ 30.0;
return avg;
}
int main()
{
int N = 3;
cout << findAverage(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static double findAverage( int N)
{
double avg = (( 6 * N * N * N * N) +
( 15 * N * N * N) +
( 10 * N * N) - 1 ) / 30.0 ;
return avg;
}
public static void main(String args[])
{
int N = 3 ;
System.out.print(findAverage(N));
}
}
|
Python3
def findAverage(N):
avg = (( 6 * N * N * N * N) + ( 15 * N * N * N) + ( 10 * N * N) - 1 ) / 30
return avg
N = 3
print ( round (findAverage(N), 4 ))
|
C#
using System;
class GFG{
static double findAverage( int N)
{
double avg = ((6 * N * N * N * N) +
(15 * N * N * N) +
(10 * N * N) - 1) / 30.0;
return avg;
}
public static void Main()
{
int N = 3;
Console.WriteLine(findAverage(N));
}
}
|
Javascript
<script>
function findAverage( N)
{
let avg = ((6 * N * N * N * N)
+ (15 * N * N * N)
+ (10 * N * N) - 1)
/ 30.0;
return avg;
}
let N = 3;
document.write( findAverage(N).toFixed(4));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...