Given a series:
Sn = 1*3 + 3*5 + 5*7 + …
It is required to find the sum of first n terms of this series represented by Sn, where n is given taken input.
Examples:
Input : n = 2
Output : S<sub>n</sub> = 18
Explanation:
The sum of first 2 terms of Series is
1*3 + 3*5
= 3 + 15
= 18
Input : n = 4
Output : S<sub>n</sub> = 116
Explanation:
The sum of first 4 terms of Series is
1*3 + 3*5 + 5*7 + 7*9
= 3 + 15 + 35 + 63
= 116
Let, the n-th term be denoted by tn.
This problem can easily be solved by observing that the nth term can be founded by following method:
tn = (n-th term of (1, 3, 5, … ) )*(nth term of (3, 5, 7, ….))
Now, n-th term of series 1, 3, 5 is given by 2*n-1
and, the n-th term of series 3, 5, 7 is given by 2*n+1
Putting these two values in tn:
tn = (2*n-1)*(2*n+1) = 4*n*n-1
Now, the sum of first n terms will be given by :
Sn = ∑(4*n*n – 1)
=∑4*{n*n}-∑(1)
Now, it is known that the sum of first n terms of series n*n (1, 4, 9, …) is given by: n*(n+1)*(2*n+1)/6
And sum of n number of 1’s is n itself.
Now, putting values in Sn:
Sn = 4*n*(n+1)*(2*n+1)/6 – n
= n*(4*n*n + 6*n – 1)/3
Now, Sn value can be easily found by putting the desired value of n.
Below is the implementation of the above approach:
C++
// C++ program to find sum of first n terms #include <bits/stdc++.h> using namespace std; int calculateSum( int n) { // Sn = n*(4*n*n + 6*n - 1)/3 return (n * (4 * n * n + 6 * n - 1) / 3); } int main() { // number of terms to be included in the sum int n = 4; // find the Sn cout << "Sum = " << calculateSum(n); return 0; } |
Java
// Java program to find sum // of first n terms class GFG { static int calculateSum( int n) { // Sn = n*(4*n*n + 6*n - 1)/3 return (n * ( 4 * n * n + 6 * n - 1 ) / 3 ); } // Driver Code public static void main(String args[]) { // number of terms to be // included in the sum int n = 4 ; // find the Sn System.out.println( "Sum = " + calculateSum(n)); } } // This code is contributed by Bilal |
Python
# Python program to find sum # of first n terms def calculateSum(n): # Sn = n*(4*n*n + 6*n - 1)/3 return (n * ( 4 * n * n + 6 * n - 1 ) / 3 ); # Driver Code # number of terms to be # included in the sum n = 4 # find the Sn print ( "Sum =" ,calculateSum(n)) # This code is contributed by Bilal |
C#
// C# program to find sum // of first n terms using System; class GFG { static int calculateSum( int n) { // Sn = n*(4*n*n + 6*n - 1)/3 return (n * (4 * n * n + 6 * n - 1) / 3); } // Driver code static public void Main () { // number of terms to be // included in the sum int n = 4; // find the Sn Console.WriteLine( "Sum = " + calculateSum(n)); } } // This code is contributed // by mahadev |
Javascript
<script> // Javascript program to find sum // of first n terms function calculateSum( n) { // Sn = n*(4*n*n + 6*n - 1)/3 return (n * (4 * n * n + 6 * n - 1) / 3); } // Driver Code // number of terms to be // included in the sum let n = 4; // find the Sn document.write( "Sum = " + calculateSum(n)); // This code contributed by Princi Singh </script> |
PHP
<?php // PHP program to find sum // of first n terms function calculateSum( $n ) { // Sn = n*(4*n*n + 6*n - 1)/3 return ( $n * (4 * $n * $n + 6 * $n - 1) / 3); } // number of terms to be // included in the sum $n = 4; // find the Sn echo "Sum = " . calculateSum( $n ); // This code is contributed // by ChitraNayal ?> |
Sum = 116
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
METHOD 2:Using list comprehension .
APPROACH:
This program calculates the sum of the series 1*3 + 3*5 + … using a list comprehension to generate the terms of the series and then finding their sum. The input value n determines the number of terms in the series to be generated and added. The sum of the series is then printed as the output.
ALGORITHM:
1.Take input value for n.
2.Generate the series using a list comprehension and store it in the series list.
3.Calculate the sum of the series list using the sum() function and store it in the variable sum.
4.Print the value of sum as the output.
C++
#include <iostream> #include <vector> int main() { int n = 4; std::vector< int > series; // Generate the series using list comprehension for ( int i = 0; i < n; i++) { int term = (2 * i + 1) * (2 * i + 3); series.push_back(term); } // Calculate the sum of the series int sum = 0; for ( int i = 0; i < series.size(); i++) { sum += series[i]; } // Print the sum std::cout << "Sum of the series: " << sum << std::endl; return 0; } // This code is contributed by uomkar369 |
Java
import java.util.ArrayList; import java.util.List; public class Main { /** * This program generates the series of 2 * i + 1 * (2 * i + 3) for i in range(0, n). * * @param n The number of terms in the series. */ public static void main(String[] args) { int n = 4 ; // The number of terms in the series. // Generate the series. List<Integer> series = new ArrayList<>(); // A list to store the terms of the series. for ( int i = 0 ; i < n; i++) { // Iterate over the number of terms. int term = ( 2 * i + 1 ) * ( 2 * i + 3 ); // Calculate the term of the series. series.add(term); // Add the term to the list. } // Calculate the sum of the series. int sum = 0 ; // The sum of the series. for ( int i = 0 ; i < series.size(); i++) { // Iterate over the list of terms. sum += series.get(i); // Add the current term to the sum. } // Print the sum. System.out.println( "Sum of the series: " + sum); // Print the sum of the series. } } |
Python3
# Using a list comprehension n = 4 series = [( 2 * i + 1 ) * ( 2 * i + 3 ) for i in range (n)] sum = sum (series) print ( "Sum of the series:" , sum ) |
C#
using System; using System.Collections.Generic; class GFG { static void Main() { int n = 4; List< int > series = new List< int >(); // Generate the series using list comprehension for ( int i = 0; i < n; i++) { int term = (2 * i + 1) * (2 * i + 3); series.Add(term); } // Calculate the sum of the series int sum = 0; foreach ( int item in series) { sum += item; } // Print the sum Console.WriteLine( "Sum of the series: " + sum); } } // This code is contributed by uomkar369 |
Javascript
let n = 4; let series = new Array(); // Generate the series using list comprehension for (let i = 0; i < n; i++) { let term = (2 * i + 1) * (2 * i + 3); series.push(term); } // Calculate the sum of the series let sum = 0; for (let i = 0; i < series.length; i++) { sum += series[i]; } // Prlet the sum document.write( "Sum of the series: " + sum); |
Sum of the series: 116
Time Complexity:
The time complexity of this program is O(n), where n is the input value. This is because the program generates n terms of the series and then calculates their sum using the sum() function, which has a time complexity of O(n).
Space Complexity:
The space complexity of this program is also O(n), where n is the input value. This is because the program generates n terms of the series and stores them in the series list, which has a space complexity of O(n). The variable sum also requires constant space, so it does not affect the space complexity.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!