Write a Program to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2

Posted in

Write a Program to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2
vinaykhatri

Vinay Khatri
Last updated on April 25, 2024

    Problem Statement

    In this programming tutorial, we will learn how to implement a program to find the sum of the following series. 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2 The series is a sum of odd square values. And the n represents the number of odd numbers that should be present in the series.

    Example 1

    Input

    n=4

    Output

    84

    Explanation

    if n = 4
    So the last digit will be (2*n-1) = (2*4-1) = 7
    The series would become 1 2 + 32 + 52 + 72

    Example 2 Input

    n=9

    Output

    969

    Explanation

    if n = 49
    So the last digit will be (2*9-1) = (2*9-1) = 17
    1 9 25 49 81 121 169 225 289 = 969

    Approach 1 (Brute Force)

    In our first approach, we will use the brute force approach, in which we will use a for loop to compute the sum of the series. The algorithm for this solution is pretty straightforward we will create a loop from range 1 to (2*n-1), increase the loop counter with 2 and add the square of every element to the total sum.

    C Program to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2

    #include <iostream>
    #include <math.h>
    
    int main()
    {	
    	int sum=0,n,i;
    	
    	printf("Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2 \n");
    	//input the value of n
    	printf("Enter the value of n: ");
    	scanf("%d", &n);
    	
    	for(i=1; i<=(2*n)-1; i+=2)
    	{
    		sum += pow(i,2);
    
    	}
    	printf("The sum of the series is: %d",sum);
    
    	return 0;
    }

    Output

    Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2
    Enter the value of n: 10
    The sum of the series is: 1330

    C++ Program to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2

    #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
    	int sum=0,n;
    	cout<<"Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2 \n";
    	
    	//input the value of n
    	cout<<"Enter the value of n: "; cin>>n;
    	for(int i=1; i<=(2*n)-1; i+=2)
    	{
    		sum += pow(i,2);
    	}
    	cout<<"The sum of the series is: "<<sum;
    	return 0; 
    }

    Output:

    Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2 
    Enter the value of n: 10 
    The sum of the series is: 1330

    Python Program to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2

    import math
    sum=0
    print("Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2")
    
    # input the value of n
    n = int(input("Enter the value of n: "))
    
    for i in range(1,((2*n)-1)+1,2):
        sum+=math.pow(i,2)
    
    print("The sum of the series is:", int(sum))

    Output:

    Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2
    Enter the value of n: 3
    The sum of the series is: 35

    Complexity Analysis

    • Time Complexity: O(N), where N represent the total number of numbers present in the series.
    • Space complexity: O(1), the space complexity of the above program is constant.

    Approach 2 (using a mathematical formula)

    In the first approach, we are using a for loop which makes the time complexity of the program O(N).  But using the mathematical formula (n * (2 * n - 1) * (2 * n + 1)) / 3 we can make our program more efficient with the time complexity of O(1).

    Example Input

    n= 4

    Output

    84

    Formula

    1^2+3^2+5^2+…..+(2n-1)^2 = (n * (2 * n - 1) * (2 * n + 1)) / 3 

    Explanation

    =(4* (2*4-1)*(2*4+1))/3
    =(4*(7 )*(9))/3
    =(252)/3
    =84

    C Program to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {	
    	int sum=0,n,i;
    	
    	printf("Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2 \n");
    	//input the value of n
    	printf("Enter the value of n: ");
    	scanf("%d", &n);
    	
    	// compute the sum
    	sum = (n * (2 * n - 1) * (2 * n + 1)) / 3;
    	
    	printf("The sum of the series is: %d",sum);
    
    	return 0;
    }

    Output

    Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2
    Enter the value of n: 5
    The sum of the series is: 165

    C++ Program to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2

    #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
    	int sum=0,n;
    	cout<<"Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2 \n";
    	
    	//input the value of n
    	cout<<"Enter the value of n: "; cin>>n;
    	
    	// compute the sum
    	sum = (n * (2 * n - 1) * (2 * n + 1)) / 3;
    	
    	cout<<"The sum of the series is: "<<sum;
    	return 0; 
    }

    Python Program to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2

    import math
    sum=0
    print("Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2")
    
    # input the value of n
    n = int(input("Enter the value of n: "))
    
    # compute the sum of the series
    sum =(n * (2 * n - 1) * (2 * n + 1)) // 3
    
    print("The sum of the series is:", int(sum))

    Output

    Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ ((2*n)-1)^2
    Enter the value of n: 4
    The sum of the series is: 84

    Complexity Analysis

    • Time Complexity: O(1), constant time complexity.
    • Space complexity: O(1).

    Wrapping Up

    In this programming tutorial, we learned how to write code in C, C++, and Python to find the sum of series 1^2+3^2+5^2+…..+2n-1^2. In this tutorial, we have used two approaches to solve the above problem. In the first approach, we used brute force where we used a loop to calculate the sum of the series. Which makes the time complexity of the above program O(N). In the second approach, we optimize the code and solve the sum problem with the formula (n * (2 * n - 1) * (2 * n + 1))/3 . The second approach solves the problem in constant time complexity.

    People are also reading:

    Leave a Comment on this Post

    0 Comments