Sum of fourth powers of first n odd natural numbers

Write a program to find sum of fourth power of first n odd natural numbers.
14 + 34 + 54 + 74 + 94 + 114 ………….+(2n-1)4.
Examples:

Input  :   3
Output :   707
14 +34 +54 =  707
Input  :   6
Output :   24310
14 + 34 + 54 + 74 + 94 + 114 

Naive Approach :- In this Simple finding the fourth powers of the first n odd natural numbers is iterate a loop from 1 to n time, and result store in variable sum.
Ex.-n=3 then, (1*1*1*1)+(3*3*3*3)+(5*5*5*5) = 707



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find the sum of fourth powers
// of first n odd natural numbers
#include <bits/stdc++.h>
using namespace std;
  
// calculate the sum of fourth power of first
// n odd natural numbers
long long int oddNumSum(int n)
{
    int j = 0;
    long long int sum = 0;
    for (int i = 1; i <= n; i++) {
        j = (2 * i - 1);
        sum = sum + (j * j * j * j);
    }
    return sum;
}
  
// Driven Program
int main()
{
    int n = 6;
    cout << oddNumSum(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 odd natural numbers
import java.io.*;
  
class GFG {
  
    // calculate the sum of
    // fourth power of first
    // n odd natural numbers
    static long oddNumSum(int n)
    {
        int j = 0;
        long sum = 0;
        for (int i = 1; i <= n; i++) {
            j = (2 * i - 1);
            sum = sum + (j * j * j * j);
        }
        return sum;
    }
  
    // Driven Program
    public static void main(String args[])
    {
        int n = 6;
        System.out.println(oddNumSum(n));
    }
}
  
// This code is contributed
// by Nikita tiwari.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to find the
# sum of fourth powers of
# first n odd natural numbers
  
# calculate the sum of 
# fourth power of first  
# n odd natural numbers
def oddNumSum(n) :
    j = 0
    sm = 0
    for i in range(1, n + 1) :
        j = (2 * i - 1)
        sm = sm + (j * j * j * j)
      
    return sm
      
# Driven Program
n = 6;
print(oddNumSum(n))
  
  
# This code is contributed 
# by Nikita tiwari.

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 odd natural numbers
using System;
  
class GFG {
  
    // calculate the sum of
    // fourth power of first
    // n odd natural numbers
    static long oddNumSum(int n)
    {
        int j = 0;
        long sum = 0;
        for (int i = 1; i <= n; i++) {
            j = (2 * i - 1);
            sum = sum + (j * j * j * j);
        }
        return sum;
    }
  
    // Driven Program
    public static void Main()
    {
        int n = 6;
        Console.Write(oddNumSum(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 odd natural 
// numbers
  
// calculate the sum of 
// fourth power of first
// n odd natural numbers
function oddNumSum($n)
{
    $j = 0;
    $sum = 0;
    for ($i = 1; $i <= $n; $i++)
    {
        $j = (2 * $i - 1);
        $sum = $sum + ($j * $j * $j * $j);
    }
    return $sum;
}
  
// Driver Code
$n = 6;
echo(oddNumSum($n));
  
// This code is contributed by Ajit.
?>

chevron_right



Output:

24310

Time Complexity : O(N)

Efficient Approach :- An efficient solution is to use direct mathematical formula which is :

Fourth power natural number = (14 + 24 + 34 + ………… +n4)
= (n(n+1)(2n+1)(3n2+3n-1))/30

Fourth power even natural number = (24 + 44 + 64 + ………… +2n4)
= 8(n(n+1)(2n+1)(3n2+3n-1))/15;

We need odd natural number so we subtract the

(Fourth power odd natural number) = (Fourth power first n natural number) – (Fourth power even natural number)
= (14 + 24 + 34 + ………… +n4) – (24 + 44 + 64 + ………… +2n4)
= (14 + 34 + 54 + ………… +(2n-1)4)
drives formula
= (2n(2n+1)(4n+1)(12n2+6n-1))/30 – (8(n(n+1)(2n+1)(3n2+3n -1)))/15
= 2n(2n+1)/30[(4n+1)(12n2+6n-1) – ((8n+8)((3n2+3n-1))]
= n(2n+1)/15[(48n3 + 24n 2 – 4n + 12n2 + 6n -1) – (24n3 + 24n 2 – 8n + 24n2 + 24n -8) ]
= n(2n+1)/15[24n3 – 12n2 – 14n + 7]

    Sum of fourth power of first n odd numbers =  n(2n+1)/15[24n3 - 12n2 - 14n + 7]

C++

filter_none
edit close
play_arrow
link brightness_4 code
// CPP Program to find the sum of fourth powers
// of first n odd natural numbers
#include <bits/stdc++.h>
using namespace std;
  
// calculate the sum of fourth power of first
// n odd natural numbers
long long int oddNumSum(int n)
{
    return (n * (2 * n + 1) * 
    (24 * n * n * n - 12 * n 
    * n - 14 * n + 7)) / 15;
}
  
// Driven Program
int main()
{
    int n = 4;
    cout << oddNumSum(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 odd 
// natural numbers
class GFG {
      
    // calculate the sum of fourth 
    // power of first n odd natural
    // numbers
    static long oddNumSum(int n)
    {
        return (n * (2 * n + 1) * 
         (24 * n * n * n - 12 * n 
          * n - 14 * n + 7)) / 15;
    }
  
    // Driven Program
    public static void main(String[] args)
    {
        int n = 4;
          
        System.out.println(oddNumSum(n));
    }
}
  
// This code is contributed by
// Smitha Dinesh Semwal.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to find the 
# sum of fourth powers of first
# n odd natural numbers
  
# calculate the sum of fourth 
# power of first n odd natural
#numbers
def oddNumSum(n):
  
    return (n * (2 * n + 1) * 
      (24 * n * n * n - 12 * n
      * n - 14 * n + 7)) / 15
  
# Driven Program
n = 4
print(int(oddNumSum(n))) 
  
# This code is contributed by
# Smitha Dinesh Semwal.

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
// odd natural numbers
using System;
  
class GFG {
  
    // calculate the sum of fourth
    // power of first n odd
    // natural numbers
    static long oddNumSum(int n)
    {
        return (n * (2 * n + 1) * 
        (24 * n * n * n - 12 * n 
        * n - 14 * n + 7)) / 15;
    }
  
    // Driven Program
    public static void Main()
    {
        int n = 4;
        Console.Write(oddNumSum(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 odd natural
// numbers
  
// calculate the sum of 
// fourth power of first
// n odd natural numbers
function oddNumSum($n)
{
    return ($n * (2 * $n + 1) * 
           (24 * $n * $n * $n
            12 * $n * $n - 14 * 
            $n + 7)) / 15;
}
  
// Driver Code
$n = 4;
echo(oddNumSum($n));
  
// This code is contributed by Ajit.
?>

chevron_right



Output:

  3108

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.





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.