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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find nth term of the
// given series
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find nth
// term of the given series
  
// Function to find nth term 
// of the given series
function 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 = sqrt(abs($d));
    $x1 = (-$b + $sqrt_val) / (2 * $a);
    $x2 = (-$b - $sqrt_val) / (2 * $a);
  
    // Ascii of 'a' is 97
    if((int)$x1 >= 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
?>

chevron_right


Output:

e
x


My Personal Notes arrow_drop_up


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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.