Sum of first N terms of Quadratic Sequence 3 + 7 + 13 + …
Given a quadratic series as given below, the task is to find sum of first n terms of this series.
Sn = 3 + 7 + 13 + 21 + 31 + ….. + upto n terms
Examples:
Input: N = 3 Output: 23 Input: N = 4 Output: 44
Approach:
Let the series be represented as
S_{n} = 3 + 7 + 13 + ... + t_{n}
where
- S_{n} represents the sum of the series till n terms.
- t_{n} represents the nth term of the series.
Now, to formulate the series, the elements need to be formed by taking the difference of the consecutive elements of the series.
Equation 1: Sn = 3 + 7 + 13 + 21 + 31 +…..+ tn-1 + tn
Equation 2: Sn = 0 + 3 + 7 + 13 + 21 + 31 + …… + tn-1 + tn
(writing the above series by shifting all elements to right by 1 position)
Now, Substract Equation 2 from Equation 1 i.e. (Equation 1 – Equation 2)
Sn – Sn = (3 – 0) + (7 – 3) + (13 – 7) + (31 – 21) + …… + (tn- tn-1) – tn
=> 0 = 3 + 4 + 6 + 8 + 10 + …… + (tn – tn-1) – tn
In above series, leaving 3 aside, terms starting from 4 to (tn – tn-1) will form an A.P.
Since formula of sum of n terms of A.P. is:
Sn = n*(2*a + (n – 1)*d)/2
which implies,
In series: 4 + 6 + 8 + … + (tn – tn-1)
AP is formed with (n-1) terms.
Hence,
Sum of this series: (n-1)*(2*4 + (n-2)*2)/2
Therefore, the original series:
0 = 3 + (n-1)*(2*4 + (n-2)*2)/2 – tn
where tn = n^2 + n + 1 which is the nth term.
Therefore,
Sum of first n terms of series will be:
tn = n^2 + n + 1
Sn = (n^2) + n + (1)
Sn = n*(n+1)*(n+2)/6 + n*(n+1)/2 + n
Sn = n*(n^2 + 3*n + 5)/3
Below is the implementation of above approach:
C++
// C++ program to find sum of first n terms #include <bits/stdc++.h> using namespace std; int calculateSum( int n) { // Sum = n*(n^2 + 3*n + 5)/3 return n * ( pow (n, 2) + 3 * n + 5) / 3; } int main() { // number of terms to be included in the sum int n = 3; // find the Sum cout << "Sum = " << calculateSum(n); return 0; } |
Java
// Java program to find sum of first n terms import java.util.*; class solution { //function to calculate sum of n terms of the series static int calculateSum( int n) { // Sum = n*(n^2 + 3*n + 5)/3 return n * ( int ) (Math.pow(n, 2 ) + 3 * n + 5 )/ 3 ; } public static void main(String arr[]) { // number of terms to be included in the sum int n = 3 ; // find the Sum System.out.println( "Sum = " +calculateSum(n)); } } |
Python3
# Python 3 program to find sum
# of first n terms
from math import pow
def calculateSum(n):
# Sum = n*(n^2 + 3*n + 5)/3
return n * (pow(n, 2) + 3 * n + 5) / 3
if __name__ == ‘__main__’:
# number of terms to be included
# in the sum
n = 3
# find the Sum
print(“Sum =”, int(calculateSum(n)))
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to find sum of first n terms using System; class gfg { public double calculateSum( int n) { // Sum = n*(n^2 + 3*n + 5)/3 return (n * (Math.Pow(n, 2) + 3 * n + 5) / 3); } } //driver code class geek { public static int Main() { gfg g = new gfg(); // number of terms to be included in the sum int n = 3; //find the Sum Console.WriteLine( "Sum = {0}" , g.calculateSum(n)); return 0; } } |
PHP
<?php // PHP program to find sum // of first n terms function calculateSum( $n ) { // Sum = n*(n^2 + 3*n + 5)/3 return $n * (pow( $n , 2) + 3 * $n + 5) / 3; } // Driver Code // number of terms to be // included in the sum $n = 3; // find the Sum echo "Sum = " . calculateSum( $n ); // This code is contributed by mits ?> |
Sum = 23
Recommended Posts:
- Print n terms of Newman-Conway Sequence
- Print first N terms of Lower Wythoff sequence
- Find n terms of Fibonacci type series with given first two terms
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Maximum and Minimum value of a quadratic function
- Program to find the Roots of Quadratic equation
- Program to find number of solutions in Quadratic Equation
- Sum of P terms of an AP if Mth and Nth terms are given
- Sum of the first N terms of the series 2,10, 30, 68,....
- Sum of the first N terms of the series 5,12, 23, 38....
- Sum of the first N terms of the series 2, 6, 12, 20, 30....
- Sum of the series 5+55+555+.. up to n terms
- Sum of first n terms of a given series 3, 6, 11, .....
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
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.