Given a series of numbers 1, 2, 4, 3, 5, 7, 9, 6, 8, 10, 11, 13… The task is to find the sum of all the numbers in series till N-th number.
Input: N = 4
1 + 2 + 4 + 3 = 10
Input: N = 10
Approach: The series is basically 20 odd numbers, 21 even numbers, 22 even numbers…. The sum of first N odd numbers is N * N and sum of first N even numbers is (N * (N+1)). Calculate the summation for 2i odd or even numbers and keep adding them to the sum.
Iterate for every power of 2, till the number of iterations exceeds N, and keep adding the respective summation of odd or even numbers according to the parity. For every segment the sum of the segment will be, (current sum of X odd/even numbers – previous sum of Y odd/even numbers), where X is the total sum of odd/even numbers till this segment and Y is the summation of odd/even numbers till the previous when odd/even numbers occurred.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Find the Sum of the series 1/2 - 2/3 + 3/4 - 4/5 + ... till N terms
- Find the Sum of the series 1, 2, 3, 6, 9, 18, 27, 54, ... till N terms
- Find the Sum of the series 1 + 1/3 + 1/5 + 1/7 + ... till N terms
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Program to print the series 1, 9, 17, 33, 49, 73, 97... till N terms
- Find the Sum of the series 1 + 2 + 9 + 64 + 625 + 7776 ... till N terms
- Program to print triangular number series till n
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- n-th term in series 2, 12, 36, 80, 150....
- N-th term in the series 1, 11, 55, 239, 991,....
- n-th term of series 1, 17, 98, 354......
- Sum of first n term of Series 3, 5, 9, 17, 33....
- Find the Nth term of the series 2 + 6 + 13 + 23 + . . .
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.