Given series 1 2 2 3 3 3 4 4 4 4 …., find n-th term of the series. The “pattern” is obvious. There is one “1”, two “2”s three “3”s, etc.

Examples:

Input : n = 5 Output : 3 Input : n = 7 Output : 4

A **naive approach** is to run two loops one from 1 to n and the other from 1 to i, and for every iteration of the inner loop keep a count, whenever the count reaches n, we can break out of both the loops and i will be our answer.

**Time Complexity:** O(n)

An **efficient approach** will be to note down a small observation which is:

The trick is to find a pattern.

Consider numbering the given sequence as follows:

1 is at position 1

2 is at positions 2, 3

3 is at positions 4, 5, 6

4 is at positions 7, 8, 9, 10

and so on…

Notice that the last positions of the individual values form a sequence.

**1, 3, 6, 10, 15, 21… **

If we want a formula for the “nth” term, start by looking at it the other way around. In which term does the number “n” first appear? Taking the first term to be the “0th” term. “1” appears in term 0, “2” appears in term 1, “3” appears in term 1+2=3, “4” appears in term 1+2+3= 6, etc. The number “x” first appears in term 1 + 2 + …+ (x- 2)+ (x-1) = x(x-1)/2.

So solving for the n-th term we get **n = x*(x-1)/2 **

Solving it using quadratic equation we get

x = ( ( 1 + sqrt(1+8*n) )/2 )

n in every case is **NOT** an integer which means the n-th number is not the first of a sequence of the same integer, but it is clear that the n-th integer is the integer value.

## C++

`// CPP program to find the nth term of the series ` `// 1 2 2 3 3 3 ... ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to solve the quadratic equation ` `int` `term(` `int` `n) ` `{ ` ` ` `// calculating the Nth term ` ` ` `int` `x = (((1) + (` `double` `)` `sqrt` `(1 + (8 * n))) / 2); ` ` ` `return` `x; ` `} ` ` ` `// driver code to check the above function ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << term(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the nth ` `// term of the series 1 2 2 3 3 3 ... ` `import` `java.io.*; ` ` ` `class` `Series { ` ` ` ` ` `// function to solve the quadratic ` ` ` `// equation ` ` ` `static` `int` `term(` `int` `n) ` ` ` `{ ` ` ` `// calculating the Nth term ` ` ` `int` `x = (((` `1` `) + (` `int` `)Math.sqrt(` `1` `+ ` ` ` `(` `8` `* n))) / ` `2` `); ` ` ` `return` `x; ` ` ` `} ` ` ` ` ` `// driver code to check the above function ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(term(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Chinmoy Lenka ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find the nth term ` `# of the series 1 2 2 3 3 3 ... ` `import` `math ` ` ` `# function to solve the quadratic equation ` `def` `term( n ): ` ` ` ` ` `# calculating the Nth term ` ` ` `x ` `=` `(((` `1` `) ` `+` `math.sqrt(` `1` `+` `(` `8` `*` `n))) ` `/` `2` `) ` ` ` `return` `x ` ` ` `# Driver code ` `n ` `=` `5` `print` `(` `int` `(term(n))) ` ` ` `# This code is contributed by Sharad_Bhardwaj. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the nth ` `// term of the series 1 2 2 3 3 3 ... ` `using` `System; ` ` ` `class` `Series ` `{ ` ` ` `// function to solve the quadratic ` ` ` `// equation ` ` ` `static` `int` `term(` `int` `n) ` ` ` `{ ` ` ` `// calculating the Nth term ` ` ` `int` `x = (((1) + (` `int` `)Math.Sqrt(1 + (8 * n))) / 2); ` ` ` `return` `x; ` ` ` `} ` ` ` ` ` `// driver code to check the above function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.WriteLine(term(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the ` `// nth term of the series ` `// 1 2 2 3 3 3 ... ` ` ` `// function to solve the ` `// quadratic equation ` `function` `term(` `$n` `) ` `{ ` ` ` `// calculating the Nth term ` ` ` `$x` `= (((1) + (double)sqrt(1 + ` ` ` `(8 * ` `$n` `))) / 2); ` ` ` `return` `$x` `; ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `echo` `((int)term(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

Time Complexity: O(log(n)) as sqrt function takes O(log n).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Program to find Nth term in the given Series
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,...
- Program to find Nth term of series 9, 23, 45, 75, 113...
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
- Find Nth term of the series 5, 13, 25, 41, 61...
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
- Find Nth term of the series 3, 14, 39, 84...
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
- Find Nth term of the series 1, 8, 54, 384...
- Find Nth term of the series 1, 1, 2, 6, 24...

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.