Sunday, October 6, 2024
Google search engine
HomeData Modelling & AIFind the sum of series 3, 7, 13, 21, 31….

Find the sum of series 3, 7, 13, 21, 31….

Given a number N. The task is to find the sum of below series upto nth term.
 

3, 7, 13, 21, 31, …. 
 

Examples
 

Input : N = 3
Output : 23

Input : N = 25
Output : 5875

 

Approach:

    Let $$S=0+3+7+13+21+31+.......+a_{n-1}+a_n$$ $$S=3+7+13+21+31+...+a_{n-2}+a_{n-1}+a_n$$

Subtracting the above two equations, we have: 
 

image

Below is the implementation of the above approach:
 

C++




// C++ Program to find the sum of given series
 
#include <iostream>
#include <math.h>
 
using namespace std;
 
// Function to calculate sum
int findSum(int n)
{
    // Return sum
    return (n * (pow(n, 2) + 3 * n + 5)) / 3;
}
 
// Driver code
int main()
{
    int n = 25;
 
    cout << findSum(n);
 
    return 0;
}


Java




// Java program to find sum of
// n terms of the given series
import java.util.*;
 
class GFG
{
static int calculateSum(int n)
{
    // returning the final sum
    return (n * ((int)Math.pow(n, 2) + 3 *
                               n + 5)) / 3;
}
 
// Driver Code
public static void main(String arr[])
{
    // number of terms to
    // find the sum
    int n = 25;
    System.out.println(calculateSum(n));
}
}
 
// This code is contributed
// by Surendra_Gangwar


Python 3




# Python program to find the
# sum of given series
 
# Function to calculate sum
def findSum(n):
    # Return sum
    return (n*(pow(n, 2)+3 * n + 5))/3
 
# driver code
n = 25
 
print(int(findSum(n)))


C#




// C# program to find
// sum of n terms of
// the given series
using System;
 
class GFG
{
static int calculateSum(int n)
{
    // returning the final sum
    return (n * ((int)Math.Pow(n, 2) + 3 *
                               n + 5)) / 3;
}
 
// Driver Code
public static void Main()
{
    // number of terms to
    // find the sum
    int n = 25;
    Console.WriteLine(calculateSum(n));
}
}
 
// This code is contributed
// by inder_verma.


PHP




<?php
// PHP Program to find the
// sum of given series
 
// Function to calculate sum
function findSum($n)
{
    // Return sum
    return ($n * (pow($n, 2) +
             3 * $n + 5)) / 3;
}
 
// Driver code
$n = 25;
 
echo findSum($n);
 
// This code is contributed
// by inder_verma
?>


Javascript




<script>
 
// javascript program to find sum of
// n terms of the given series
  
 
function calculateSum(n)
{
    // returning the final sum
    return (n * (parseInt(Math.pow(n, 2) + 3 *
                               n + 5)) / 3);
}
 
// Driver Code
// number of terms to
// find the sum
var n = 25;
document.write(calculateSum(n));
 
 
// This code contributed by shikhasingrajput
 
</script>


Output: 

5875

 

Time Complexity : O(1)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments