Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....

22 Jun, 2022
Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5.... The sequence is built in the following way: at first the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on. Find the number on the n-th position of the sequence.Examples :  
Input : n = 3
Output : 2
The 3rd number in the sequence is 2.

Input : 55
Output : 10
 
First Approach: The idea is to first find block number of n. To determine the block with the n-th number, we first subtract 1 (count of elements in first block) from n, then subtract 2, then subtract 3 and so on until we got negative n. The number of subtractions will be the number of the block and the position in the block will be the last non-negative number we will get.  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find the// value at n-th place in// the given sequence#include <bits/stdc++.h>using namespace std; // Returns n-th number in sequence// 1, 1, 2, 1, 2, 3, 1, 2, 4, ...int findNumber(int n){    n--;     // One by one subtract counts    // elements in different blocks    int i = 1;    while (n >= 0)    {        n -= i;        ++i;    }     return (n + i);} // Driver codeint main(){    int n = 3;    cout << findNumber(n) << endl;    return 0;}
// Java program to find the// value at n-th place in// the given sequence import java.io.*; class GFG{     // Returns n-th number in sequence    // 1, 1, 2, 1, 2, 3, 1, 2, 4, ...    static int findNumber(int n)    {        n--;         // One by one subtract counts        // elements in different blocks        int i = 1;        while (n >= 0)        {            n -= i;            ++i;        }         return (n + i);    }     // Driver Code    public static void main(String[] args)    {        int n = 3;                 System.out.println(findNumber(n));    }} // This code is contributed by Ajit.
# Python code to find the value at# n-th place in the given sequence # Returns n-th number in sequence# 1, 1, 2, 1, 2, 3, 1, 2, 4, ...def findNumber( n ):         n -= 1         # One by one subtract counts    # elements in different blocks    i = 1    while n >= 0:        n -= i        i += 1    return (n + i) # Driver coden = 3print(findNumber(n)) # This code is contributed# by "Sharad_Bhardwaj".
// C# program to find the// value at n-th place in// the given sequenceusing System; class GFG{     // Returns n-th number in sequence    // 1, 1, 2, 1, 2, 3, 1, 2, 4, ...    static int findNumber(int n)    {        n--;         // One by one subtract counts        // elements in different blocks        int i = 1;        while (n >= 0)        {            n -= i;            ++i;        }         return (n + i);    }         // Driver code    public static void Main()    {        int n = 3;        Console.WriteLine(findNumber(n));    }} // This code is contributed by vt_m.
<?php// PHP program to find the// value at n-th place in// the given sequence  // Returns n-th number in sequence// 1, 1, 2, 1, 2, 3, 1, 2, 4, ...function findNumber( $n){    $n--;     // One by one subtract counts    // elements in different blocks    $i = 1;    while ($n >= 0)    {        $n -= $i;        ++$i;    }     return ($n + $i);} // Driver code$n = 3;echo findNumber($n); // This code is contributed by anuj_67.?>
<script>    // Javascript program to find the    // value at n-th place in    // the given sequence         // Returns n-th number in sequence    // 1, 1, 2, 1, 2, 3, 1, 2, 4, ...    function findNumber(n)    {        n--;         // One by one subtract counts        // elements in different blocks        let i = 1;        while (n >= 0)        {            n -= i;            ++i;        }        return (n + i);    } // Driver code    let n = 3;    document.write(findNumber(n));         // This code is contributed by mukesh07.</script>
Output :  
2
Time Complexity: O(√n)Second Approach: The answer to the Infinite Sequence may be done in O(1). We may organize the sequence as: 1 (1). That means the positions that the first one in that line is 1, 2 (2) 1, 2, 3 (4) 1, 2, 3, 4 (7) 1, 2, 3, 4, 5 (11) Then we would have a new sequence, which is much easier to find. 1 (1) 2 (2) 4 (3) 7 (4) 11 The number in the parenthesis are the distance between the number of the new sequence. To find the base of the number we need to solve the equation n = x(x+1)/2 + 1 [OR x^2 + x + 2 – 2n = 0]. We need to isolate x (get the max floor value). Then we use the x we got in the same formula, but now the result is going to be the base for the line. We only need to calculate the distance between the number we receive as input and the number we got as base. 
n — base + 1
 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find the// value at n-th place in// the given sequence#include <bits/stdc++.h>using namespace std; // Definition of findNumber// functionint findNumber(int n){    // Finding x from equation    // n = x(x + 1)/2 + 1    int x = (int)floor((-1 +             sqrt(1 + 8 * n - 8)) / 2);     // Base of current block    int base = (x * (x + 1)) / 2 + 1;     // Value of n-th element    return n - base + 1;} // Driver codeint main(){    int n = 55;    cout << findNumber(n) << endl;    return 0;}
// Java program to find the// value at n-th place in// the given sequence import java.io.*; class GFG{     // Definition of findNumber function    static int findNumber(int n)    {                 // Finding x from equation        // n = x(x + 1)/2 + 1        int x = (int)Math.floor((-1 +                Math.sqrt(1 + 8 * n - 8)) / 2);         // Base of current block        int base = (x * (x + 1)) / 2 + 1;         // Value of n-th element        return n - base + 1;    }     // Driver code    public static void main(String[] args)    {        int n = 55;                 System.out.println(findNumber(n));    }} // This code is contributed by Ajit.
# Python program to find# the value at n-th place# in the given sequenceimport math # Definition of findNumber functiondef findNumber( n ):         # Finding x from equation    # n = x(x + 1)/2 + 1    x = int(math.floor((-1 + math.sqrt(1            + 8 * n - 8)) / 2))     # Base of current block    base = (x * (x + 1)) / 2 + 1         # Value of n-th element    return n - base + 1 # Driver coden = 55print(findNumber(n)) # This code is contributed# by "Abhishek Sharma 44"
// C# program to find the// value at n-th place in// the given sequenceusing System; class GFG{     // Definition of findNumber function    static int findNumber(int n)    {         // Finding x from equation        // n = x(x + 1)/2 + 1        int x = (int)Math.Floor((-1 +        Math.Sqrt(1 + 8 * n - 8)) / 2);         // Base of current block        int Base = (x * (x + 1)) / 2 + 1;         // Value of n-th element        return n - Base + 1;    }     // Driver code    public static void Main()    {        int n = 55;        Console.WriteLine(findNumber(n));    }} // This code is contributed by vt_m.
<?php// PHP program to find the// value at n-th place in// the given sequence  // Definition of findNumber functionfunction findNumber($n){    // Finding x from equation    // n = x(x + 1)/2 + 1    $x = floor((-1 + sqrt(1 +                8 * $n - 8)) / 2);     // Base of current block    $base = ($x * ($x + 1)) / 2 + 1;     // Value of n-th element    return $n - $base + 1;} // Driver code$n = 55;echo findNumber($n) ; // This code is contributed by anuj_67.?>
<script>    // Javascript program to find the    // value at n-th place in    // the given sequence         // Definition of findNumber    // function    function findNumber(n)    {        // Finding x from equation        // n = x(x + 1)/2 + 1        let x = Math.floor((-1 + Math.sqrt(1 + 8 * n - 8)) / 2);         // Base of current block        let base = (x * (x + 1)) / 2 + 1;         // Value of n-th element        return n - base + 1;    }         let n = 55;    document.write(findNumber(n)); // This code is contributed by suresh07.</script>
Output :  
10 
Time Complexity : O(1)This article is contributed by Sagar Shukla. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
mukesh07
suresh07
kumargaurav97520
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.