Sum of fourth powers of the first n natural numbers - GeeksforGeeks

31 Mar, 2021
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++
Java
Python3
C#
PHP
Javascript
// CPP Program to find the sum of forth powers// of first n natural numbers#include <bits/stdc++.h>using namespace std; // Return the sum of forth power of first n// natural numberslong 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;} // Driven Programint main(){    int n = 6;    cout << fourthPowerSum(n) << endl;    return 0;}
// Java Program to find the// sum of forth powers of// first n natural numbersimport java.io.*;import java.util.*; class GFG {         // Return the sum of forth    // power of first n natural    // numbers    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));         }} // This code is contributed by Gitanjali.
# Python3 Program to find the# sum of forth powers of first# n natural numbersimport math # Return the sum of forth power of# first n natural numbersdef fourthPowerSum( n):     sum = 0    for i in range(1, n+1) :        sum = sum + (i * i * i * i)    return sum# Driver methodn=6print (fourthPowerSum(n)) # This code is contributed by Gitanjali.
// C# program to find the// sum of forth powers of// first n natural numbersusing System; class GFG {         // Return the sum of forth power    // of first n natural numbers    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));         }} // This code is contributed by vt_m.
<?php// PHP Program to find th// sum of fourth powers// of first n natural numbers // Return the sum of fourth// power of first n// natural numbersfunction fourthPowerSum($n){    $sum = 0;    for ($i = 1; $i <= $n; $i++)        $sum = $sum + ($i * $i * $i * $i);    return $sum;} // Driver Code$n = 6;echo(fourthPowerSum($n)); // This code is contributed by Ajit.?>
<script> // javascript Program to find the sum of forth powers// of first n natural numbers // Return the sum of forth power of first n// natural numbersfunction fourthPowerSum( n){    let sum = 0;    for (let i = 1; i <= n; i++)        sum = sum + (i * i * i * i);    return sum;}// Driven Program     let n = 6;     document.write(fourthPowerSum(n)); // This code contributed by aashish1995 </script>
Output:
2275
Time Complexity : O(n)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++
Java
Python3
C#
PHP
Javascript
// CPP Program to find the sum of forth power of first// n natural numbers#include <bits/stdc++.h>using namespace std; // Return the sum of forth power of first n natural// numberslong long int fourthPowerSum(int n){    return ((6 * n * n * n * n * n) +            (15 * n * n * n * n) +            (10 * n * n * n) - n) / 30;} // Driven Programint main(){    int n = 6;    cout << fourthPowerSum(n) << endl;    return 0;}
// Java Program to find the// sum of forth powers of// first n natural numbersimport java.io.*;import java.util.*; class GFG {         // Return the sum of    // forth power of first    // n natural numbers    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));         }} // This code is contributed by Gitanjali.
# Python3 Program to# find the sum of# forth powers of# first n natural numbersimport math # Return the sum of# forth power of# first n natural# numbersdef fourthPowerSum(n):     return ((6 * n * n * n * n * n) +            (15 * n * n * n * n) +            (10 * n * n * n) - n) / 30     # Driver methodn=6print (fourthPowerSum(n)) # This code is contributed by Gitanjali.
// C# Program to find the// sum of forth powers of// first n natural numbersusing System; class GFG {         // Return the sum of    // forth power of first    // n natural numbers    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));         }} // This code is contributed by vt_m.
<?php// PHP Program to find the sum// of fourth power of first// n natural numbers // Return the sum of fourth// power of first n natural// numbersfunction fourthPowerSum($n){    return ((6 * $n * $n * $n * $n * $n) +            (15 * $n * $n * $n * $n) +            (10 * $n * $n * $n) - $n) / 30;} // Driver Code$n = 6;echo(fourthPowerSum($n)); // This code is contributed by Ajit.?>
<script> // javascript Program to find the// sum of forth powers of// first n natural numbers     // Return the sum of// forth power of first// n natural numbersfunction 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)); // This code is contributed by 29AjayKumar </script>
Output: 
2275
Time Complexity : O(1) 
jit_t
aashish1995
29AjayKumar
number-theory
series
series-sum
Mathematical
number-theory
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Sieve of Eratosthenes
Print all possible combinations of r elements in a given array of size n
Program for Decimal to Binary Conversion
The Knight's tour problem | Backtracking-1
Operators in C / C++
Program for factorial of a number