Skip to content
Related Articles

Related Articles

Improve Article

Sum of fifth powers of the first n natural numbers

  • Difficulty Level : Basic
  • Last Updated : 26 Mar, 2021

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 : 
15 + 25 + 35 + 45 + 52 + 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++




// CPP Program to find the sum of fifth powers
// of first n natural numbers
#include <bits/stdc++.h>
using namespace std;
 
// calculate the sum of fifth power of
// first n natural numbers
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;
}
 
// Driven Program
int main()
{
    int n = 6;
    cout << fifthPowerSum(n) << endl;
    return 0;
}

Java




// Java Program to find the
// sum of fifth  powers of
// first n natural numbers
import java.io.*;
 
class GFG
{
    // calculate the sum of fifth
    // power of first n natural
    // numbers
    static long fifthPowerSum(int n)
    {
        long sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + (i * i * i * i * i);
        return sum;
    }
     
    // Driven Program
    public static void main(String args[])
    {
        int n = 6;
        System.out.println(fifthPowerSum(n));
    }
}
 
// This code is contributed by
// Nikita Tiwari.

Python3




# Python 3 Program to find the
# sum of fifth powers of first
# n natural numbers
 
# calculate the sum of fifth
# power of first n natural
# numbers
def fifthPowerSum(n) :
 
    sm = 0
     
    for i in range(1, n+1) :
        sm = sm + (i * i * i * i * i)
     
    return sm
 
# Driven Program
n = 6
print(fifthPowerSum(n))
 
# This code is contributed
# by Nikita Tiwari.

C#




// C# Program to find the
// sum of fifth powers of
// first n natural numbers
using System;
 
class GFG
{
    // calculate the sum of fifth
    // power of first n natural
    // numbers
    static long fifthPowerSum(int n)
    {
        long sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + (i * i * i * i * i);
 
        return sum;
    }
     
    // Driven Program
    public static void Main()
    {
        int n = 6;
        Console.Write(fifthPowerSum(n));
    }
}
 
// This code is contributed by
// vt_m.

PHP




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




<script>
 
// javascript 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)
{
    let sum = 0;
    for (let i = 1; i <= n; i++)
        sum = sum + (i * i * i * i * i);
    return sum;
}
 
// Driven Program
 
    let n = 6;
     document.write(fifthPowerSum(n));
 
// This code contributed by aashish1995
 
</script>

Output: 
 

 12201

Time complexity : O(N) 
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++




// CPP Program to find the sum of fifth power
// of first n natural numbers
#include <bits/stdc++.h>
using namespace std;
 
// calculate the sum of fifth power of first n natural numbers
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;
}
 
// Driven Program
int main()
{
    int n = 5;
    cout << fifthPowerSum(n) << endl;
    return 0;
}

Java




// Java Program to find the sum of fifth power
// of first n natural numbers
import java.io.*;
 
class GFG {
     
    // calculate the sum of fifth power
    //of first n natural numbers
    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;
    }
     
    // Driven Program
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(fifthPowerSum(n));
    }
}
 
 /*This code is contributed by Nikita Tiwari.*/

Python3




# Python 3 Program to find the
# sum of fifth power of first
# n natural numbers
 
# Calculate the sum of fifth
# power of first n natural
# numbers
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
 
 
# Driven Program
n = 5
print(fifthPowerSum(n))
 
# This code is contributed by Nikita Tiwari.

C#




// C# Program to find the sum
// of fifth power of first n
// natural numbers
using System;
 
class GFG {
     
    // calculate the sum of fifth power
    // of first n natural numbers
    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;
    }
     
    // Driven Program
    public static void Main()
    {
        int n = 5;
        Console.Write(fifthPowerSum(n));
    }
}
 
/*This code is contributed by vt_m.*/

PHP




<?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)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :