Sum of fourth power of first n even natural numbers

Write a program to find the sum of fourth power of first n even natural numbers.
24 + 44 + 64 + 84 + 104 +………+2n4
Examples:

Input  :   3
Output :  1568
24 +44 +64 = 1568
Input  :   6
Output :  36400
24 + 44 + 64 + 84 + 104 + 124 

Naive Approach :- In this Simple finding the fourth powers of the first n even natural numbers is iterate a loop from 1 to n time. Every i’th iteration store in variable and continue till (i!=n). This is take a O(N) Time Complexity.



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find the sum of fourth powers of
// first n even natural numbers
#include <bits/stdc++.h>
using namespace std;
  
// calculate the sum of fourth power of first n even
// natural numbers
long long int evenPowerSum(int n)
{
    long long int sum = 0;
    for (int i = 1; i <= n; i++) {
  
        // made even number
        int j = 2 * i;
        sum = sum + (j * j * j * j);
    }
    return sum;
}
  
// Driven Program
int main()
{
    int n = 5;
    cout << evenPowerSum(n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find the sum of fourth powers of
// first n even natural numbers
  
import java.io.*;
  
class GFG {
      
    // calculate the sum of fourth power of first 
    // n even natural numbers
    static long evenPowerSum(int n)
    {
        long sum = 0;
        for (int i = 1; i <= n; i++)
        {
      
            // made even number
            int j = 2 * i;
            sum = sum + (j * j * j * j);
        }
          
        return sum;
    }
  
    // Driven Program
    public static void main (String[] args) {
          
        int n = 5;
        System.out.println(evenPowerSum(n));
    }
}
  
/*This code is contributed by vt_m.*/

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find 
# the sum of fourth powers of
# first n even natural numbers
  
# calculate the sum of fourth 
# power of first n even
# natural numbers
def evenPowerSum(n):
    sum = 0;
    for i in range(1, n + 1):
          
        # made even number
        j = 2 * i;
        sum = sum + (j * j * j * j);
    return sum;
  
# Driver Code
n = 5;
print(evenPowerSum(n));
  
# This is contributed by mits.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



Output:

 15664

Time Complexity : O(n)

Efficient Approach :- An efficient solution is to use direct mathematical formula which is derived below, This is take only O(1) Time Complexity.

Sum of fourth power of first n even natural number = 8*(n*(n+1)*(2*n+1)(3*n2+3*n -1))/15

How does this formula work?
Sum of fourth power of natural numbers is = (n(n+1)(2n+1)(3n2+3n-1))/30
we need even natural number so we multiply each term 24
= 24(14 + 24 + 34 + ………… +n4)
= (24 + 44 + 64 + ………… +2n4)
= 24*(sum of fourth power natural number)
= 16*(n*(n+1)*(2*n+1)(3*n2+3*n -1))/30
= 8*(n*(n+1)*(2*n+1)(3*n2+3*n -1))/15

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find the sum of fourth powers of
// first n even natural numbers
#include <bits/stdc++.h>
using namespace std;
  
// calculate the sum of fourth power of first n 
// even natural numbers
long long int evenPowerSum(int n)
{
    return (8 * n * (n + 1) * (2 * n + 1) * 
          (3 * n * n + 3 * n - 1)) / 15;
}
  
// Driven Program
int main()
{
    int n = 4;
    cout << evenPowerSum(n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA Program to find the sum of fourth powers of
// first n even natural numbers
  
import java.io.*;
  
class GFG {
          
    // calculate the sum of fourth power of first n 
    // even natural numbers
    static long evenPowerSum(int n)
    {
        return (8 * n * (n + 1) * (2 * n + 1) * 
                   (3 * n * n + 3 * n - 1)) / 15;
    }
      
    // Driven Program
    public static void main (String[] args) {
          
        int n = 4;
        System.out.println(evenPowerSum(n));
    }
}
  
/* This code is contributed by vt_m. */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find 
# the sum of fourth powers 
# of first n even natural 
# numbers
  
# calculate the sum of 
# fourth power of first n 
# even natural numbers
def evenPowerSum(n):
    return (8 * n * (n + 1) * 
           (2 * n + 1) * (3 *
            n * n + 3 * n - 1)) / 15;
  
# Driver Code
n = 4;
print (int(evenPowerSum(n)));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find the sum of fourth powers of
// first n even natural numbers
  
using System;
  
class GFG {
  
    // calculate the sum of fourth power of first n
    // even natural numbers
    static long evenPowerSum(int n)
    {
        return (8 * n * (n + 1) * (2 * n + 1) *
                     (3 * n * n + 3 * n - 1)) / 15;
    }
  
    // Driven Program
    public static void Main()
    {
        int n = 4;
          
        Console.Write(evenPowerSum(n));
    }
}
  
/* This code is contributed by vt_m.*/

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find the
// sum of fourth powers of
// first n even natural numbers
  
// calculate the sum of 
// fourth power of first n 
// even natural numbers
function evenPowerSum($n)
{
    return (8 * $n * ($n + 1) * 
           (2 * $n + 1) * 
           (3 * $n * $n + 3 * 
            $n - 1)) / 15;
}
  
// Driver Code
$n = 4;
echo(evenPowerSum($n));
  
// This code is contributed by Ajit.
?>

chevron_right



Output:

5664

Time Complexity : O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.