Skip to content
Related Articles

Related Articles

Improve Article

Program to find the sum of the series 23+ 45+ 75+….. upto N terms

  • Difficulty Level : Medium
  • Last Updated : 18 Mar, 2021

Given a number N, the task is to find the Nth term of the below series:
 

23 + 45 + 75 + 113 + 159 +…… upto N terms

Examples: 
 

Input: N = 4
Output: 256
Explanation:
Nth term = (2 * N * (N + 1) * (4 * N + 17) + 54 * N) / 6
         = (2 * 4 * (4 + 1) * (4 * 4 + 17) + 54 * 4) / 6
         = 256

Input: N = 10
Output: 2180

 

Approach:
The Nth term of the given series can be generalized as: 
T_n = ( 2 * n + 3 )^2 - 2 * n\\ T_n = 4 * n^2+ 10*n+9
Sum of first n terms of this series: 
$S_n=4\sum_{i=1}^n n^2+10\sum_{i=1}^n n +9\sum_{i=1}^n 1\\\\ S_n=4\frac{n(n+1)(2n+1)}{6}+30\frac{n(n+1)}{2} +9n\\\\ $
Therefore, Sum of first n terms of this series: 



*** QuickLaTeX cannot compile formula:
 

*** Error message:
Error: Nothing to show, formula is empty

Below is the implementation of the above approach: 
 

C++




// CPP program to find sum
// upto N-th term of the series:
// 23, 45, 75, 113...
 
#include <iostream>
using namespace std;
 
// calculate Nth term of series
int findSum(int N)
{
    return (2 * N * (N + 1) * (4 * N + 17) + 54 * N) / 6;
}
 
// Driver Function
int main()
{
 
    // Get the value of N
    int N = 4;
 
    // Get the sum of the series
    cout << findSum(N) << endl;
 
    return 0;
}

Java




// Java program to find sum
// upto N-th term of the series:
// 23, 45, 75, 113...
import java.util.*;
 
class solution
{
     
static int findSum(int N)
{
    //return the final sum
    return (2 * N * (N + 1) * (4 * N + 17) + 54 * N) / 6;
}
 
//Driver program
public static void main(String arr[])
{
// Get the value of N
    int N = 4;
 
// Get the sum of the series
 System.out.println(findSum(N));
 
}
}

Python3




# Python3 program to find sum
# upto N-th term of the series:
# 23, 45, 75, 113...
 
# calculate Nth term of series
def findSum(N):
     
    return (2 * N * (N + 1) * (4 * N + 17) + 54 * N) / 6
     
#Driver Function
if __name__=='__main__':
#Get the value of N
    N = 4
 
#Get the sum of the series
    print(findSum(N))
 
#this code is contributed by Shashank_Sharma

C#




// C# program to find sum
// upto N-th term of the series:
// 23, 45, 75, 113...
using System;
 
class GFG
{
     
static int findSum(int N)
{
    //return the final sum
    return (2 * N * (N + 1) *
           (4 * N + 17) + 54 * N) / 6;
}
 
// Driver Code
static void Main()
{
    // Get the value of N
    int N = 4;
     
    // Get the sum of the series
    Console.Write(findSum(N));
}
}
 
// This code is contributed by Raj

PHP




<?php
// PHP program to find sum
// upto N-th term of the series:
// 23, 45, 75, 113...
 
// calculate Nth term of series
function findSum($N)
{
    return (2 * $N * ($N + 1) *
           (4 * $N + 17) + 54 * $N) / 6;
}
 
// Driver Code
 
// Get the value of N
$N = 4;
 
// Get the sum of the series
echo findSum($N);
 
// This code is contributed
// by anuj_67
?>

Javascript




<script>
// javascript rogram to find sum
// upto N-th term of the series:
// 23, 45, 75, 113...
 
// calculate Nth term of series
function findSum( N)
{
    return (2 * N * (N + 1) * (4 * N + 17) + 54 * N) / 6;
}
 
// Driver Function
 
    // Get the value of N
    let N = 4;
 
    // Get the sum of the series
    document.write(findSum(N));
     
// This code is contributed by Rajput-Ji
 
</script>
Output: 
256

 

Time Complexity: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :