Program to find the sum of the series 23+ 45+ 75+….. upto N terms
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:
Sum of first n terms of this series:
Therefore, Sum of first n terms of this series:
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int findSum( int N)
{
return (2 * N * (N + 1) * (4 * N + 17) + 54 * N) / 6;
}
int main()
{
int N = 4;
cout << findSum(N) << endl;
return 0;
}
|
Java
import java.util.*;
class solution
{
static int findSum( int N)
{
return ( 2 * N * (N + 1 ) * ( 4 * N + 17 ) + 54 * N) / 6 ;
}
public static void main(String arr[])
{
int N = 4 ;
System.out.println(findSum(N));
}
}
|
Python3
def findSum(N):
return ( 2 * N * (N + 1 ) * ( 4 * N + 17 ) + 54 * N) / 6
if __name__ = = '__main__' :
N = 4
print (findSum(N))
|
C#
using System;
class GFG
{
static int findSum( int N)
{
return (2 * N * (N + 1) *
(4 * N + 17) + 54 * N) / 6;
}
static void Main()
{
int N = 4;
Console.Write(findSum(N));
}
}
|
PHP
<?php
function findSum( $N )
{
return (2 * $N * ( $N + 1) *
(4 * $N + 17) + 54 * $N ) / 6;
}
$N = 4;
echo findSum( $N );
?>
|
Javascript
<script>
function findSum( N)
{
return (2 * N * (N + 1) * (4 * N + 17) + 54 * N) / 6;
}
let N = 4;
document.write(findSum(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1) since using constant variables
Last Updated :
19 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...