Write a program to find the sum of fourth power of first n even natural numbers.
24 + 44 + 64 + 84 + 104 +………+2n4
Examples:
Input: 3
Output: 1568
24 +44 +64 = 1568
Input: 6
Output: 36400
24 + 44 + 64 + 84 + 104 + 124
Naive Approach :- In this Simple finding the fourth powers of the first n even natural numbers is iterate a loop from 1 to n time. Every i’th iteration store in variable and continue till (i!=n). This takes a O(N) Time Complexity.
C++
#include <bits/stdc++.h>
using namespace std;
long long int evenPowerSum( int n)
{
long long int sum = 0;
for ( int i = 1; i <= n; i++) {
int j = 2 * i;
sum = sum + (j * j * j * j);
}
return sum;
}
int main()
{
int n = 5;
cout << evenPowerSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static long evenPowerSum( int n)
{
long sum = 0 ;
for ( int i = 1 ; i <= n; i++)
{
int j = 2 * i;
sum = sum + (j * j * j * j);
}
return sum;
}
public static void main (String[] args) {
int n = 5 ;
System.out.println(evenPowerSum(n));
}
}
|
Python3
def evenPowerSum(n):
sum = 0 ;
for i in range ( 1 , n + 1 ):
j = 2 * i;
sum = sum + (j * j * j * j);
return sum ;
n = 5 ;
print (evenPowerSum(n));
|
C#
using System;
class GFG {
static long evenPowerSum( int n)
{
long sum = 0;
for ( int i = 1; i <= n; i++) {
int j = 2 * i;
sum = sum + (j * j * j * j);
}
return sum;
}
public static void Main()
{
int n = 5;
Console.Write(evenPowerSum(n));
}
}
|
PHP
<?php
function evenPowerSum( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
$j = 2 * $i ;
$sum = $sum + ( $j * $j * $j * $j );
}
return $sum ;
}
$n = 5;
echo (evenPowerSum( $n ));
?>
|
Javascript
<script>
function evenPowerSum( n)
{
let sum = 0;
for (let i = 1; i <= n; i++)
{
let j = 2 * i;
sum = sum + (j * j * j * j);
}
return sum;
}
let n = 5;
document.write(evenPowerSum(n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Efficient Approach :- An efficient solution is to use direct mathematical formula which is derived below, This takes only O(1) Time Complexity.
Sum of fourth power of first n even natural number = 8*(n*(n+1)*(2*n+1)(3*n2+3*n -1))/15
How does this formula work?
Sum of fourth power of natural numbers is = (n(n+1)(2n+1)(3n2+3n-1))/30
we need even natural number so we multiply each term 24
= 24(14 + 24 + 34 + ………… +n4)
= (24 + 44 + 64 + ………… +2n4)
= 24*(sum of fourth power natural number)
= 16*(n*(n+1)*(2*n+1)(3*n2+3*n -1))/30
= 8*(n*(n+1)*(2*n+1)(3*n2+3*n -1))/15
C++
#include <bits/stdc++.h>
using namespace std;
long long int evenPowerSum( int n)
{
return (8 * n * (n + 1) * (2 * n + 1) *
(3 * n * n + 3 * n - 1)) / 15;
}
int main()
{
int n = 4;
cout << evenPowerSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static long evenPowerSum( int n)
{
return ( 8 * n * (n + 1 ) * ( 2 * n + 1 ) *
( 3 * n * n + 3 * n - 1 )) / 15 ;
}
public static void main (String[] args) {
int n = 4 ;
System.out.println(evenPowerSum(n));
}
}
|
Python3
def evenPowerSum(n):
return ( 8 * n * (n + 1 ) *
( 2 * n + 1 ) * ( 3 *
n * n + 3 * n - 1 )) / 15 ;
n = 4 ;
print ( int (evenPowerSum(n)));
|
C#
using System;
class GFG {
static long evenPowerSum( int n)
{
return (8 * n * (n + 1) * (2 * n + 1) *
(3 * n * n + 3 * n - 1)) / 15;
}
public static void Main()
{
int n = 4;
Console.Write(evenPowerSum(n));
}
}
|
PHP
<?php
function evenPowerSum( $n )
{
return (8 * $n * ( $n + 1) *
(2 * $n + 1) *
(3 * $n * $n + 3 *
$n - 1)) / 15;
}
$n = 4;
echo (evenPowerSum( $n ));
?>
|
Javascript
<script>
function evenPowerSum(n)
{
return (8 * n * (n + 1) * (2 * n + 1)
* (3 * n * n + 3 * n - 1)) / 15;
}
var n = 4;
document.write(evenPowerSum(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!