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: 40Input: 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 = 3S_{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++ 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 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 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;
}
}
# 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
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
<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