Find the sequence number of a triangular number - GeeksforGeeks

17 Mar, 2021
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 tringular number are: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55.  
 
Examples:  
Input: N = 21 Output:6 Explanation: Since 15 is a 6th Tringular Number.Input: N = 12 Output:-1 Explanation: Since 12 is not a tringular Number  
 
Approach:  
Since tringular numbers are sum of natural numbers so can be generalise as quadratic equation.
Since tringular numbers are sum of natural numbers so can be generalise as quadratic equation.
 
C++
Java
Python3
C#
Javascript
// 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 tringular number    if (B != A)        cout << "-1";    else        cout << B;} // This code is contributed by yatinagg
// Java code to print sequence// number of a triangular numberimport 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 code to print sequence# number of a triangular number import math N = 21A = math.sqrt(2 * N + 0.25)-0.5B = int(A) # if N is not tringular numberif B != A:    print(-1)else:    print(B)        
// C# code to print sequence// number of a triangular numberusing 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
<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>
6
 
Time Complexity: O(1) Auxiliary Space: O(1) 
yatinagg
Code_Mech
Akanksha_Rai
nidhi_biet
Rajput-Ji
series
series-sum
Competitive Programming
Computer Science Quizzes
Mathematical
Write From Home
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Breadth First Traversal ( BFS ) on a 2D array
Count of triplets in an Array (i, j, k) such that i < j < k and a[k] < a[i] < a[j]
Runtime Errors
Most important type of Algorithms
Multistage Graph (Shortest Path)
Shortest path in a directed graph by Dijkstra’s algorithm
Check whether two strings contain same characters in same order
Difference between Backtracking and Branch-N-Bound technique
Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
String hashing using Polynomial rolling hash function