Write a program to find the sum of fourth powers of the first n natural numbers 14 + 24 + 34 + 44 + …….+ n4 till n-th term.
Examples :
Input : 4
Output : 354
14 + 24 + 34 + 44 = 354
Input : 6
Output : 2275
14 + 24 + 34 + 44+ 54+ 64 = 2275
Naive Approach :- Simple finding the fourth powers of the first n natural numbers is iterate a loop from 1 to n time. like suppose n=4.
(1*1*1*1)+(2*2*2*2)+(3*3*3*3)+(4*4*4*4) = 354
C++
#include <bits/stdc++.h>
using namespace std;
long long int fourthPowerSum( int n)
{
long long int sum = 0;
for ( int i = 1; i <= n; i++)
sum = sum + (i * i * i * i);
return sum;
}
int main()
{
int n = 6;
cout << fourthPowerSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static long fourthPowerSum( int n)
{
long sum = 0 ;
for ( int i = 1 ; i <= n; i++)
sum = sum + (i * i * i * i);
return sum;
}
public static void main (String[] args)
{
int n = 6 ;
System.out.println(fourthPowerSum(n));
}
}
|
Python3
import math
def fourthPowerSum( n):
sum = 0
for i in range ( 1 , n + 1 ) :
sum = sum + (i * i * i * i)
return sum
n = 6
print (fourthPowerSum(n))
|
C#
using System;
class GFG {
static long fourthPowerSum( int n)
{
long sum = 0;
for ( int i = 1; i <= n; i++)
sum = sum + (i * i * i * i);
return sum;
}
public static void Main ()
{
int n = 6;
Console.WriteLine(fourthPowerSum(n));
}
}
|
PHP
<?php
function fourthPowerSum( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
$sum = $sum + ( $i * $i * $i * $i );
return $sum ;
}
$n = 6;
echo (fourthPowerSum( $n ));
?>
|
Javascript
<script>
function fourthPowerSum( n)
{
let sum = 0;
for (let i = 1; i <= n; i++)
sum = sum + (i * i * i * i);
return sum;
}
let n = 6;
document.write(fourthPowerSum(n));
</script>
|
Output:
2275
Complexity Analysis:
Time Complexity : O(n) ,as there is single loop used inside fourthpowersum() function.
Space Complexity: O(1) , as there is no extra space used.
Efficient Approach :- An efficient solution is to use direct mathematical formula which is 1/30n(n+1)(2n+1)(3n2+3n+1) or it is also write (1/5)n5 + (1/2)n4 + (1/3)n3 – (1/30)n. This solution take O(1) time.
C++
#include <bits/stdc++.h>
using namespace std;
long long int fourthPowerSum( int n)
{
return ((6 * n * n * n * n * n) +
(15 * n * n * n * n) +
(10 * n * n * n) - n) / 30;
}
int main()
{
int n = 6;
cout << fourthPowerSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static long fourthPowerSum( int n)
{
return (( 6 * n * n * n * n * n) +
( 15 * n * n * n * n) +
( 10 * n * n * n) - n) / 30 ;
}
public static void main (String[] args)
{
int n = 6 ;
System.out.println(fourthPowerSum(n));
}
}
|
Python3
import math
def fourthPowerSum(n):
return (( 6 * n * n * n * n * n) +
( 15 * n * n * n * n) +
( 10 * n * n * n) - n) / 30
n = 6
print (fourthPowerSum(n))
|
C#
using System;
class GFG {
static long fourthPowerSum( int n)
{
return ((6 * n * n * n * n * n) +
(15 * n * n * n * n) +
(10 * n * n * n) - n) / 30;
}
public static void Main ()
{
int n = 6;
Console.Write(fourthPowerSum(n));
}
}
|
PHP
<?php
function fourthPowerSum( $n )
{
return ((6 * $n * $n * $n * $n * $n ) +
(15 * $n * $n * $n * $n ) +
(10 * $n * $n * $n ) - $n ) / 30;
}
$n = 6;
echo (fourthPowerSum( $n ));
?>
|
Javascript
<script>
function fourthPowerSum(n)
{
return ((6 * n * n * n * n * n) +
(15 * n * n * n * n) +
(10 * n * n * n) - n) / 30;
}
var n = 6;
document.write(fourthPowerSum(n));
</script>
|
Output:
2275
Complexity Analysis:
Time Complexity : O(1)
Space Complexity: 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!