Horje
Sum of fifth powers of the first n natural numbers

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++

<?php
// PHP Program to find
// the sum of fifth powers
// of first n natural numbers
 
// calculate the sum of
// fifth power of
// first n natural numbers
function fifthPowerSum($n)
{
    $sum = 0;
    for ($i = 1; $i <= $n; $i++)
        $sum = $sum + ($i * $i * $i *
                             $i * $i);
    return $sum;
}
 
// Driver Code
$n = 6;
echo(fifthPowerSum($n));
 
// This code is contributed by Ajit.
?>

Javascript

<?php
// PHP Program to find
// the sum of fifth power
// of first n natural numbers
 
// calculate the sum of
// fifth power of first
// n natural numbers
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;
}
 
// Driver Code
$n = 5;
echo(fifthPowerSum($n));
 
// This code is contributed by Ajit.
?>

Javascript

<script>
// JavaScript Program to find the sum of fifth power
// of first n natural numbers
 
// calculate the sum of fifth power of first n natural numbers
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;
}
 
// Driven Program
 
    let n = 5;
    document.write(fifthPowerSum(n) + "<br>");
     
// This code is contributed by Mayank Tyagi
 
</script>

Output

4425

Time complexity: O(1)
Auxiliary Space: O(1)




Reffered: https://www.geeksforgeeks.org


Mathematical

Related
Program to compare two fractions Program to compare two fractions
Find the distance covered to collect items at equal distances Find the distance covered to collect items at equal distances
Addition and Subtraction of Matrix using pthreads Addition and Subtraction of Matrix using pthreads
Pernicious number Pernicious number
Find the distance covered to collect items at equal distances Find the distance covered to collect items at equal distances

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15