Write a program to find sum of fourth power of first n odd natural numbers.
14 + 34 + 54 + 74 + 94 + 114 ………….+(2n-1)4.
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
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)
Fourth power even natural number = (24 + 44 + 64 + ………… +2n4)
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)
= (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]
Time Complexity : O(1)
Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of fourth powers of the first n natural numbers
- Sum of fourth power of first n even natural numbers
- Sum of fifth powers of the first n natural numbers
- Sum of first N natural numbers by taking powers of 2 as negative number
- Sum of first N natural numbers which are not powers of K
- Sum of kth powers of first n natural numbers
- Sum of first N natural numbers with all powers of 2 added twice
- Find two numbers whose difference of fourth power is equal to N
- Sum of cubes of first n odd natural numbers
- Sum of range in a series of first odd then even natural numbers
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Smallest N digit number which is a perfect fourth power
- Difference between sum of the squares of first n natural numbers and square of sum
- Sum of sum of all subsets of a set formed by first N natural numbers
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of sum-series of first N Natural numbers
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Sum of cubes of even and odd natural numbers
- Sum of all odd natural numbers in range L and R
- Find k numbers which are powers of 2 and have sum N | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.