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.
Input: N = 21
Since 15 is a 6th Tringular Number.
Input: N = 12
Since 12 is not a tringular Number
- Since tringular numbers are sum of natural numbers so can be generalise as quadratic equation.
(X*(X+1))/2 = N
X = (-1 + (1 + 8*N) ) /2
X = -0.5 + (0.25 + 2*N)^1/2
Time Complexity: O(1)
Auxillary Space: O(1)
- Find Nth number in a sequence which is not a multiple of a given number
- First triangular number whose number of divisors exceeds N
- Find if the given number is present in the infinite sequence or not
- Find a sequence of N prime numbers whose sum is a composite number
- Centered triangular number
- Smallest triangular number larger than p
- Squared triangular number (Sum of cubes)
- Index of smallest triangular number with N digits
- Check if a number can be represented as a sum of 2 triangular numbers
- Program to check if N is a Centered Triangular Number
- Program to print triangular number series till n
- k-th number in the Odd-Even sequence
- Ulam Number Sequence
- Find the largest number smaller than integer N with maximum number of set bits
- Find minimum number to be divided to make a number a perfect square
- Given number of matches played, find number of teams in tournament
- Find smallest possible Number from a given large Number with same count of digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find the minimum number to be added to N to make it a prime number
- Find smallest number formed by inverting digits of given number N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.