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-
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
Illustration:
Input: N = 5
Output: 40
Explanation:
a = 2Ā
d = 3
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> |
Ā
Ā
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
Ā
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!