
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Programe to find n-th term in series 1 2 2 3 3 3 4
In this problem, we are given an integer N. The task is to find n-th term in series 1 2 2 3 3 3 4….
Let’s take an example to understand the problem,
Input
N = 6
Output
3
Explanation
The series upto nth term is 1, 2, 2, 3, 3, 3, ...
Solution Approach
A simple approach to solve the problem is by using a nested loop. The outer for loop is from 1 to n. And the inner loop is from 1 to i (iterator of outer loop). For each iteration in the inner loop, count the number of elements of the series and return the value of i when count is equal to n.
A more efficient approach to solve the problem is using pattern positions. The elements of the sequence is with their positions in the series are −
Element 1: position 1 Element 2: position 2, 3 Element 3: position 4, 5, 6 Element 4: position 7, 8, 9, 10
For these values, we can create a series using the last position of element in the series which is,
1, 3, 6, 10, 15, 21, 28, ….
x appears in term, 1 + 2 + 3 + … + (x-2) + (x-1)...
This can be generalised as n = x*(x-1)/2
2n = x2 - x => x2 - x - 2n = 0
Solve the equation using formula for solution of quadratic equation,
$$x=1/2*(1+\sqrt{1+8*n)}$$
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; int findNthTerm(int n) { int x = (((1) + (double)sqrt(1 + (8 * n))) / 2); return x; } int main(){ int n = 12; cout<<"The series is 1, 2, 2, 3, 3, 3, 4, 4, ...\n"; cout<<n<<"th term of the series is "<<findNthTerm(n); return 0; }
Output
The series is 1, 2, 2, 3, 3, 3, 4, 4, ... 12th term of the series is 5
- Related Articles
- Program to find N-th term of series 2, 4, 3, 4, 15… in C++
- Program to find N-th term of series 0, 0, 2, 1, 4, 2, 6, 3, 8…in C++
- Program to find N-th term of series 0, 2,1, 3, 1, 5, 2, 7, 3...in C++
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\n
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n! in C++
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms in C++
- Program to find N-th term of series 1, 3, 12, 60, 360...in C++
