Write a program to find the sum of Fifth powers of the first n natural numbers 15 + 25+ 35 + 45+ …….+ n5 till n-th term.
Examples:
Input : 4
Output : 1300
15 + 25 + 35 + 45 = 1300
Input : 6
Output : 12201
15 + 25 + 35 + 45 + 55 + 65
Naive Approach :- In this Simple finding the fifth powers of the first n natural numbers is iterate a loop from 1 to n time. like suppose n=5. and store in sum variable.
(1*1*1*1*1)+(2*2*2*2*2)+(3*3*3*3*3)+(4*4*4*4*4) = 1300
C++
#include <bits/stdc++.h>
using namespace std;
long long int fifthPowerSum( int n)
{
long long int sum = 0;
for ( int i = 1; i <= n; i++)
sum = sum + (i * i * i * i * i);
return sum;
}
int main()
{
int n = 6;
cout << fifthPowerSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long fifthPowerSum( int n)
{
long sum = 0 ;
for ( int i = 1 ; i <= n; i++)
sum = sum + (i * i * i * i * i);
return sum;
}
public static void main(String args[])
{
int n = 6 ;
System.out.println(fifthPowerSum(n));
}
}
|
Python3
def fifthPowerSum(n) :
sm = 0
for i in range ( 1 , n + 1 ) :
sm = sm + (i * i * i * i * i)
return sm
n = 6
print (fifthPowerSum(n))
|
C#
using System;
class GFG
{
static long fifthPowerSum( int n)
{
long sum = 0;
for ( int i = 1; i <= n; i++)
sum = sum + (i * i * i * i * i);
return sum;
}
public static void Main()
{
int n = 6;
Console.Write(fifthPowerSum(n));
}
}
|
PHP
<?php
function fifthPowerSum( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
$sum = $sum + ( $i * $i * $i *
$i * $i );
return $sum ;
}
$n = 6;
echo (fifthPowerSum( $n ));
?>
|
Javascript
<script>
function fifthPowerSum( n)
{
let sum = 0;
for (let i = 1; i <= n; i++)
sum = sum + (i * i * i * i * i);
return sum;
}
let n = 6;
document.write(fifthPowerSum(n));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach :- An efficient solution is to use direct mathematical formula which is :
(2*n6+6*n5+5*n4 - n2)/12
OR (Can also be written as)
(1/6)n6 + (1/2)n5 + (5/12)n4 – (1/12)n2.
C++
#include <bits/stdc++.h>
using namespace std;
long long int fifthPowerSum( int n)
{
return ((2 * n * n * n * n * n * n) +
(6 * n * n * n * n * n) +
(5 * n * n * n * n) -
(n * n)) / 12;
}
int main()
{
int n = 5;
cout << fifthPowerSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static long fifthPowerSum( int n)
{
return (( 2 * n * n * n * n * n * n) +
( 6 * n * n * n * n * n) +
( 5 * n * n * n * n) -
(n * n)) / 12 ;
}
public static void main(String args[])
{
int n = 5 ;
System.out.println(fifthPowerSum(n));
}
}
|
Python3
def fifthPowerSum(n) :
return (( 2 * n * n * n * n * n * n) +
( 6 * n * n * n * n * n) +
( 5 * n * n * n * n) -
(n * n)) / / 12
n = 5
print (fifthPowerSum(n))
|
C#
using System;
class GFG {
static long fifthPowerSum( int n)
{
return ((2 * n * n * n * n * n * n) +
(6 * n * n * n * n * n) +
(5 * n * n * n * n) -
(n * n)) / 12;
}
public static void Main()
{
int n = 5;
Console.Write(fifthPowerSum(n));
}
}
|
PHP
<?php
function fifthPowerSum( $n )
{
return ((2 * $n * $n * $n * $n * $n * $n ) +
(6 * $n * $n * $n * $n * $n ) +
(5 * $n * $n * $n * $n ) -
( $n * $n )) / 12;
}
$n = 5;
echo (fifthPowerSum( $n ));
?>
|
Javascript
<script>
function fifthPowerSum(n)
{
return ((2 * n * n * n * n * n * n) +
(6 * n * n * n * n * n) +
(5 * n * n * n * n) -
(n * n)) / 12;
}
let n = 5;
document.write(fifthPowerSum(n) + "<br>" );
</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!