VOOZH about

URL: https://www.geeksforgeeks.org/dsa/triangular-numbers/

⇱ Triangular Numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Triangular Numbers

Last Updated : 23 Jul, 2025

A number is termed 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, second row has two points, third row has three points and so on. The starting triangular numbers are 1, 3 (1+2), 6 (1+2+3), 10 (1+2+3+4).

👁 triangular

How to check if a number is Triangular?

The idea is based on the fact that n'th triangular number can be written as sum of n natural numbers, that is n*(n+1)/2. The reason for this is simple, base line of triangular grid has n dots, line above base has (n-1) dots and so on.

Examples:

Input:10
Output:True
Explanation:10 is a triangular number because it can be written as the sum of the first 4 natural numbers: 1 + 2 + 3 + 4 = 10.

Input:8
Output:False
Explanation:8 is not a triangular number because no sum of the first n natural numbers equals 8. The closest triangular numbers are 6 (1+2+3) and 10 (1+2+3+4).

[Naive Approach] Iterative Summation Method - O(n) time and O(1) space

We start with 1 and check if the number is equal to 1. If it is not, we add 2 to make it 3 and recheck with the number. We repeat this procedure until the sum remains less than or equal to the number that is to be checked for being triangular.
Following is the implementations to check if a number is triangular number. 

Below is the implementation of above approach


Output
The number is a triangular number

[Expected Approach] Quadratic Equation Root Formula Method - O(log n) time and O(1) Space

We form a quadratic equation by equating the number to the formula of sum of first 'n' natural numbers, and if we get atleast one value of 'n' that is a natural number, we say that the number is a triangular number. 

Let the input number be 'num'. We consider,

(n*(n+1))/2 = num

as,

n2 + n + (-2 * num) = 0

Below is the implementation of above approach.


Output
The number is a triangular number
Comment