Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmFind the sum of first N terms of the series 2, 5,...

Find the sum of first N terms of the series 2, 5, 8, 11, 14..

Given a positive integer N, the task is to find the sum of the first N terms of the seriesĀ 

2, 5, 8, 11, 14..

Examples:

Input: N = 5
Output: 40

Input: N = 10
Output: 155

Ā 

Approach:

1st term = 2

2nd term = (2 + 3) = 5

3rd term = (5 + 3) = 8

4th term = (8 + 3) = 11

.

.

Nth term = (2 + (N ā€“ 1) * 3) = 3N ā€“ 1

The sequence is formed by using the following pattern. For any value N-

S_{N}=\frac{N}{2}(2*a+(N-1)d)

Here,Ā 
a is the first term
d is the common difference

The above solution can be derived following the series of steps-

The series-

2, 5, 8, 11, ā€¦., till N terms

is in A.P. with first term of the series a = 2 and common difference d = 3

Sum of N terms of an A.P. is

S_{N}=\frac{N}{2}(2*a+(N-1)d)

Illustration:

Input: N = 5
Output: 40
Explanation:
a = 2Ā 
d = 3
S_{N}=\frac{N}{2}(2*a+(N-1)d)
S_{N}=\frac{5}{2}(2*2+(5-1)3)
S_{N}=40

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
using namespace std;
Ā 
// Function to return
// Nth term of the series
int nth(int n, int a1, int d)
{
Ā Ā Ā Ā return (a1 + (n - 1) * d);
}
Ā 
// Function to return sum of
// Nth term of the series
int sum(int a1, int nterm, int n)
{
Ā Ā Ā Ā return n * (a1 + nterm) / 2;
}
Ā 
// Driver code
int main()
{
Ā Ā Ā Ā // Value of N
Ā Ā Ā Ā int N = 5;
Ā 
Ā Ā Ā Ā // First term
Ā Ā Ā Ā int a = 2;
Ā 
Ā Ā Ā Ā // Common difference
Ā Ā Ā Ā int d = 3;
Ā 
Ā Ā Ā Ā // finding last term
Ā Ā Ā Ā int nterm = nth(N, a, d);
Ā Ā Ā Ā cout << sum(a, nterm, N) << endl;
Ā Ā Ā Ā return 0;
}


C




// C program to implement
// the above approach
#include <stdio.h>
Ā 
// Function to return
// Nth term of the series
int nth(int n, int a1, int d)
{
Ā Ā Ā Ā return (a1 + (n - 1) * d);
}
Ā 
// Function to return
// sum of Nth term of the series
int sum(int a1, int nterm, int n)
{
Ā Ā Ā Ā return n * (a1 + nterm) / 2;
}
Ā 
// Driver code
int main()
{
Ā Ā Ā Ā // Value of N
Ā Ā Ā Ā int N = 5;
Ā 
Ā Ā Ā Ā // First term
Ā Ā Ā Ā int a = 2;
Ā 
Ā Ā Ā Ā // Common difference
Ā Ā Ā Ā int d = 3;
Ā 
Ā Ā Ā Ā // Finding last term
Ā Ā Ā Ā int nterm = nth(N, a, d);
Ā 
Ā Ā Ā Ā printf("%d", sum(a, nterm, N));
Ā Ā Ā Ā return 0;
}


Java




// Java program to implement
// the above approach
import java.io.*;
Ā 
class GFG {
Ā Ā Ā Ā // Driver code
Ā Ā Ā Ā public static void main(String[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Value of N
Ā Ā Ā Ā Ā Ā Ā Ā int N = 5;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // First term
Ā Ā Ā Ā Ā Ā Ā Ā int a = 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Common difference
Ā Ā Ā Ā Ā Ā Ā Ā int d = 3;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Finding Nth term
Ā Ā Ā Ā Ā Ā Ā Ā int nterm = nth(N, a, d);
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(sum(a, nterm, N));
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to return
Ā Ā Ā Ā // Nth term of the series
Ā Ā Ā Ā public static int nth(int n,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int a1, int d)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return (a1 + (n - 1) * d);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to return
Ā Ā Ā Ā // sum of Nth term of the series
Ā Ā Ā Ā public static int sum(int a1,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int nterm, int n)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return n * (a1 + nterm) / 2;
Ā Ā Ā Ā }
}


Python3




# Python code for the above approach
Ā 
# Function to return
# Nth term of the series
def nth(n, a1, d):
Ā Ā Ā Ā return (a1 + (n - 1) * d);
Ā 
# Function to return sum of
# Nth term of the series
def sum(a1, nterm, n):
Ā Ā Ā Ā return n * (a1 + nterm) / 2;
Ā 
# Driver code
Ā 
# Value of N
N = 5;
Ā 
# First term
a = 2;
Ā 
# Common difference
d = 3;
Ā 
# finding last term
nterm = nth(N, a, d);
print((int)(sum(a, nterm, N)))
Ā 
# This code is contributed by gfgking


C#




using System;
Ā 
public class GFG
{
Ā Ā Ā 
Ā Ā Ā Ā // Function to return
Ā Ā Ā Ā // Nth term of the series
Ā Ā Ā Ā public static int nth(int n, int a1, int d)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return (a1 + (n - 1) * d);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to return
Ā Ā Ā Ā // sum of Nth term of the series
Ā Ā Ā Ā public static int sum(int a1, int nterm, int n)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return n * (a1 + nterm) / 2;
Ā Ā Ā Ā }
Ā Ā Ā Ā static public void Main()
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Code
Ā Ā Ā Ā Ā Ā Ā Ā // Value of N
Ā Ā Ā Ā Ā Ā Ā Ā int N = 5;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // First term
Ā Ā Ā Ā Ā Ā Ā Ā int a = 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Common difference
Ā Ā Ā Ā Ā Ā Ā Ā int d = 3;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Finding Nth term
Ā Ā Ā Ā Ā Ā Ā Ā int nterm = nth(N, a, d);
Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(sum(a, nterm, N));
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by Potta Lokesh


Javascript




<script>
Ā Ā Ā Ā Ā Ā Ā Ā // JavaScript code for the above approach
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Function to return
Ā Ā Ā Ā Ā Ā Ā Ā // Nth term of the series
Ā Ā Ā Ā Ā Ā Ā Ā function nth(n, a1, d) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return (a1 + (n - 1) * d);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Function to return sum of
Ā Ā Ā Ā Ā Ā Ā Ā // Nth term of the series
Ā Ā Ā Ā Ā Ā Ā Ā function sum(a1, nterm, n) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return n * (a1 + nterm) / 2;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Driver code
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Value of N
Ā Ā Ā Ā Ā Ā Ā Ā let N = 5;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // First term
Ā Ā Ā Ā Ā Ā Ā Ā let a = 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Common difference
Ā Ā Ā Ā Ā Ā Ā Ā let d = 3;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // finding last term
Ā Ā Ā Ā Ā Ā Ā Ā let nterm = nth(N, a, d);
Ā Ā Ā Ā Ā Ā Ā Ā document.write(sum(a, nterm, N) + '<br>');
Ā 
Ā Ā Ā Ā Ā Ā Ā // This code is contributed by Potta Lokesh
Ā Ā Ā Ā </script>


Ā 
Ā 

Output:Ā 

40

Ā 

Time complexity: O(1) because performing constant operations

Auxiliary Space: O(1) // since no extra array or recursion is used so the space taken by the algorithm is constant
Ā 

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!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments