Get the best out of our app
GeeksforGeeks App
Open App
Browser
Continue

# Program to find N-th term of the series a, b, b, c, c, c,…….

Given a number N. The task is to write a program to find the N-th term in the below series:

`a, b, b, c, c, c, d, d, d, d, .....`

Examples:

```Input : 12
Output : e

Input : 288
Output : x```

The idea is to use AP sum formula to find the solution to this problem. Clearly the series is depicted as 1a, 2b’s, 3c’s, 4d’s, 5e’s and so on. Thus making it an AP. Now we can use the AP sum formula:

`sum = (n/2)*(a + (n-1)*d)  `

Which in this case becomes sum = (n(n+1))/2( since a = 1 and d = 1 ) where ‘sum’ here is the Nth term given.

Below is the implementation of the above approach:

## C++

 `// CPP program to find nth term of the``// given series``#include ``using` `namespace` `std;` `// Function to find nth term of the``// given series``void` `findNthTerm(``int` `n)``{``    ``// Let us find roots of equation x * (x + 1)/2 = n``    ``n = n * 2;``    ``int` `a = 1, b = 1, c = -1 * n;``    ``int` `d = b * b - 4 * a * c;``    ``double` `sqrt_val = ``sqrt``(``abs``(d));``    ``int` `x1 = (``double``)(-b + sqrt_val) / (2 * a);``    ``int` `x2 = (``double``)(-b - sqrt_val) / (2 * a);` `    ``if` `(x1 >= 1)``        ``cout << (``char``)(``'a'` `+ x1) << endl;``    ``else` `if` `(x2 >= 1)``        ``cout << (``char``)(``'a'` `+ x2) << endl;``}` `// Driver program``int` `main()``{``    ``int` `n = 12;``    ``findNthTerm(n);` `    ``n = 288;``    ``findNthTerm(n);` `    ``return` `0;``}`

## Java

 `// Java program to find nth``// term of the given series``import` `java.io.*;` `class` `GFG {` `    ``// Function to find nth term``    ``// of the given series``    ``static` `void` `findNthTerm(``int` `n)``    ``{``        ``// Let us find roots of``        ``// equation x * (x + 1)/2 = n``        ``n = n * ``2``;``        ``int` `a = ``1``, b = ``1``, c = -``1` `* n;``        ``int` `d = b * b - ``4` `* a * c;``        ``double` `sqrt_val = Math.sqrt(Math.abs(d));``        ``int` `x1 = (``int``)((-b + sqrt_val) / (``2` `* a));``        ``int` `x2 = (``int``)((-b - sqrt_val) / (``2` `* a));` `        ``if` `(x1 >= ``1``)``            ``System.out.println((``char``)(``'a'` `+ x1));``        ``else` `if` `(x2 >= ``1``)``            ``System.out.println((``char``)(``'a'` `+ x2));``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``12``;``        ``findNthTerm(n);` `        ``n = ``288``;``        ``findNthTerm(n);``    ``}``}` `// This code has been contributed``// by anuj_67.`

## Python 3

 `# Python 3 program to find nth``# term of the given series``import` `math` `# Function to find nth term``# of the given series``def` `findNthTerm(n):` `    ``# Let us find roots of``    ``# equation x * (x + 1)/2 = n``    ``n ``=` `n ``*` `2``    ``a ``=` `1``    ``b ``=` `1``    ``c ``=` `-``1` `*` `n``    ``d ``=` `b ``*` `b ``-` `4` `*` `a ``*` `c``    ``sqrt_val ``=` `math.sqrt(``abs``(d))``    ``x1 ``=` `(``-``b ``+` `sqrt_val) ``/``/` `(``2` `*` `a)``    ``x2 ``=` `(``-``b ``-` `sqrt_val) ``/``/` `(``2` `*` `a)``    ``x1 ``=` `int``(x1)``    ``x2 ``=` `int``(x2)``    ` `    ``# ASCII of 'a' is 97``    ``if` `(x1 >``=` `1``):``        ``print``(``chr``(``97``+``x1))``    ``elif` `(x2 >``=` `1``):``        ``print``(``chr``(``97``+``x2))` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``n ``=` `12``    ``findNthTerm(n)` `    ``n ``=` `288``    ``findNthTerm(n)` `# This code is contributed``# by ChitraNayal`

## Javascript

 ``

## C#

 `// C# program to find nth``// term of the given series``using` `System;` `public` `class` `GFG {` `    ``// Function to find nth term``    ``// of the given series``    ``static` `void` `findNthTerm(``int` `n)``    ``{``        ``// Let us find roots of``        ``// equation x * (x + 1)/2 = n``        ``n = n * 2;``        ``int` `a = 1, b = 1, c = -1 * n;``        ``int` `d = b * b - 4 * a * c;``        ``double` `sqrt_val = Math.Sqrt(Math.Abs(d));``        ``int` `x1 = (``int``)((-b + sqrt_val) / (2 * a));``        ``int` `x2 = (``int``)((-b - sqrt_val) / (2 * a));` `        ``if` `(x1 >= 1)``            ``Console.WriteLine((``char``)(``'a'` `+ x1));``        ``else` `if` `(x2 >= 1)``            ``Console.WriteLine((``char``)(``'a'` `+ x2));``    ``}` `    ``// Driver Code``    ``static` `public` `void` `Main(String[] args)``    ``{``        ``int` `n = 12;``        ``findNthTerm(n);` `        ``n = 288;``        ``findNthTerm(n);``    ``}``}``// contributed by Arnab Kundu`

## PHP

 `= 1)``        ``echo` `chr``(97+``\$x1``) . ``"\n"``;``    ``else` `if` `((int)``\$x2` `>= 1)``        ``echo` `chr``(97+``\$x2``), ``"\n"``;``}` `// Driver Code``\$n` `= 12;``findNthTerm(``\$n``);` `\$n` `= 288;``findNthTerm(``\$n``);` `// This Code is contributed by mits``?>`

Output:

```e
x```

Time complexity: O(n) // Because using inbuilt function sqrt

Auxiliary Space: O(1)

Approach 2:

One approach to simplify this code and avoid solving a quadratic equation to find the nth term of the given series is to use the formula for the nth term of the series:

`nth term = ceil((-1 + sqrt(1 + 8*n)) / 2)`

here is the step by step procedure:

• If we substitute the values for a and l in this formula, we get: n = (-1 + sqrt(1 + 8*S)) / 2,where S is the sum of the first n terms.
• The above formula gives us the value of n for a given sum S. Since we want to find the nth term of the series, we can use this formula to calculate the value of n for a given input value, and then find the nth term using the formula: nth term = ‘a’ + (n – 1), where ‘a’ is the first term of the series.
• In the given program, the function findNthTerm takes an integer argument n, which represents the position of the term to be found in the series. The function calculates the value of n using the above formula and then finds the nth term of the series using the formula given above. The program calls this function for two different input values (12 and 288) to demonstrate how it works. The output of the program is the nth term of the series for each input value, which is printed to the console.

here is the given code:

## C++

 `#include ``#include ``using` `namespace` `std;` `void` `findNthTerm(``int` `n)``{``    ``int` `x1 = ``ceil``((-1 + ``sqrt``(1 + 8*n)) / 2);``    ``if` `(x1 >= 1)``        ``cout << (``char``)(``'a'` `+ x1 - 1) << endl;``}` `int` `main()``{``    ``int` `n = 12;``    ``findNthTerm(n);` `    ``n = 288;``    ``findNthTerm(n);` `    ``return` `0;``}`

## Java

 `import` `java.lang.Math;` `public` `class` `Main {``    ``public` `static` `void` `findNthTerm(``int` `n) {``        ``int` `x1 = (``int``) Math.ceil((-``1` `+ Math.sqrt(``1` `+ ``8` `* n)) / ``2``);``        ``if` `(x1 >= ``1``)``            ``System.out.println((``char``) (``'a'` `+ x1 - ``1``));``    ``}` `    ``public` `static` `void` `main(String[] args) {``        ``int` `n = ``12``;``        ``findNthTerm(n);` `        ``n = ``288``;``        ``findNthTerm(n);``    ``}``}`

## Python3

 `import` `math` `def` `findNthTerm(n):``    ``x1 ``=` `math.ceil((``-``1` `+` `math.sqrt(``1` `+` `8``*``n)) ``/` `2``)``    ``if` `x1 >``=` `1``:``        ``print``(``chr``(``ord``(``'a'``) ``+` `x1 ``-` `1``))` `n ``=` `12``findNthTerm(n)` `n ``=` `288``findNthTerm(n)`

## C#

 `using` `System;` `class` `Program``{``    ``static` `void` `findNthTerm(``int` `n)``    ``{``        ``int` `x1 = (``int``)Math.Ceiling((-1 + Math.Sqrt(1 + 8 * n)) / 2);``        ``if` `(x1 >= 1)``            ``Console.WriteLine((``char``)(``'a'` `+ x1 - 1));``    ``}` `    ``static` `void` `Main(``string``[] args)``    ``{``        ``int` `n = 12;``        ``findNthTerm(n);` `        ``n = 288;``        ``findNthTerm(n);` `        ``Console.ReadLine();``    ``}``}`

## Javascript

 `function` `findNthTerm(n) {``  ``let x1 = Math.ceil((-1 + Math.sqrt(1 + 8 * n)) / 2);``  ``if` `(x1 >= 1) {``    ``console.log(String.fromCharCode(``'a'``.charCodeAt(0) + x1 - 1));``  ``}``}` `let n = 12;``findNthTerm(n);` `n = 288;``findNthTerm(n);`

Output

```e
x
```

Time complexity: O(1) Constant  time to produce output

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up