Sunday, September 7, 2025
HomeData Modelling & AIFind the sequence number of a triangular number

Find the sequence number of a triangular number

Given an integer N print the sequence number of the given Triangular Number. If the number is not a triangular number then print -1.
 

A number is termed as a triangular number if we can represent it in the form of a triangular grid of points such that the points form an equilateral triangle and each row contains as many points as the row number, i.e., the first row has one point, the second row has two points, the third row has three points and so on. 
First 10 triangular number are: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55. 
 

Examples: 
 

Input: N = 21 
Output:
Explanation: 
Since 15 is a 6th Triangular Number.
Input: N = 12 
Output:-1 
Explanation: 
Since 12 is not a Triangular Number 

Approach: 

  1. Since triangular numbers are the sum of natural numbers so can be generalized as a quadratic equation.

C++




// C++ code to print sequence
// number of a triangular number
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    int N = 21;
    int A = sqrt(2 * N + 0.25) - 0.5;
    int B = A;
 
    // If N is not triangular number
    if (B != A)
        cout << "-1";
    else
        cout << B;
}
 
// This code is contributed by yatinagg


Java




// Java code to print sequence
// number of a triangular number
import java.util.*;
class GFG{
     
public static void main(String args[])
{
    int N = 21;
    int A = (int)(Math.sqrt(2 * N + 0.25) - 0.5);
    int B = A;
 
    // If N is not tringular number
    if (B != A)
        System.out.print("-1");
    else
        System.out.print(B);
}
}
 
// This code is contributed by Akanksha_Rai


Python3




# Python3 code to print sequence
# number of a triangular number
 
import math
 
N = 21
A = math.sqrt(2 * N + 0.25)-0.5
B = int(A)
 
# if N is not tringular number
if B != A:
    print(-1)
else:
    print(B)        


C#




// C# code to print sequence
// number of a triangular number
using System;
class GFG{
     
public static void Main()
{
    int N = 21;
    int A = (int)(Math.Sqrt(2 * N + 0.25) - 0.5);
    int B = A;
 
    // If N is not tringular number
    if (B != A)
        Console.Write("-1");
    else
        Console.Write(B);
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
// javascript code to print sequence
// number of a triangular number
    let N = 21;
    let A = Math.sqrt(2 * N + 0.25) - 0.5;
    let B = A;
 
    // If N is not tringular number
    if (B != A)
         document.write("-1");
    else
         document.write(B);
          
// This code is contributed by Rajput-Ji
 
</script>


Output: 

6

 

Time Complexity: O(logN)  as sqrt function is being used
Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Previous article
Next article
RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6644 POSTS0 COMMENTS
Nicole Veronica
11808 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11871 POSTS0 COMMENTS
Shaida Kate Naidoo
6755 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS