Finding whether a number is triangular number in JavaScript

Triangular numbers are sequences of numbers that can form an equilateral triangle. The nth triangular number is the sum of the first n natural numbers, calculated as T(n) = n × (n + 1) / 2.

Triangular Numbers Pattern: T(1) = 1 T(2) = 3 T(3) = 6 T(4) = 10 Formula: T(n) = n × (n + 1) / 2 Sequence: 1, 3, 6, 10, 15, 21, 28, 36, 45...

Problem

We need to write a JavaScript function that takes a number and returns true if it's a triangular number, false otherwise.

Method 1: Using Mathematical Formula

We can check if a number is triangular by solving the equation n × (n + 1) / 2 = num for n. If n is a positive integer, the number is triangular.

const isTriangular = (num) => {
    if (num <= 0) return false;
    
    // Using quadratic formula: n² + n - 2*num = 0
    // n = (-1 + sqrt(1 + 8*num)) / 2
    const n = (-1 + Math.sqrt(1 + 8 * num)) / 2;
    
    // Check if n is a positive integer
    return Number.isInteger(n) && n > 0;
};

// Test with various numbers
console.log(isTriangular(1));   // First triangular number
console.log(isTriangular(3));   // Second triangular number
console.log(isTriangular(6));   // Third triangular number
console.log(isTriangular(9));   // Not triangular
console.log(isTriangular(10));  // Fourth triangular number
console.log(isTriangular(15));  // Fifth triangular number
true
true
true
false
true
true

Method 2: Iterative Approach

We can also check by generating triangular numbers until we reach or exceed the target number.

const isTriangularIterative = (num) => {
    if (num <= 0) return false;
    
    let triangular = 0;
    let n = 1;
    
    while (triangular < num) {
        triangular = n * (n + 1) / 2;
        if (triangular === num) return true;
        n++;
    }
    
    return false;
};

// Test the iterative approach
console.log("Testing iterative approach:");
console.log(isTriangularIterative(21));  // 6th triangular number
console.log(isTriangularIterative(22));  // Not triangular
console.log(isTriangularIterative(36));  // 8th triangular number
Testing iterative approach:
true
false
true

Comparison

Method Time Complexity Space Complexity Accuracy
Mathematical Formula O(1) O(1) High (floating-point precision)
Iterative O(?n) O(1) Perfect (integer arithmetic)

Finding All Triangular Numbers Up to N

const getTriangularNumbers = (limit) => {
    const triangularNumbers = [];
    let n = 1;
    let triangular = 0;
    
    while (triangular <= limit) {
        triangular = n * (n + 1) / 2;
        if (triangular <= limit) {
            triangularNumbers.push(triangular);
        }
        n++;
    }
    
    return triangularNumbers;
};

console.log("Triangular numbers up to 50:");
console.log(getTriangularNumbers(50));
Triangular numbers up to 50:
[ 1, 3, 6, 10, 15, 21, 28, 36, 45 ]

Conclusion

The mathematical formula method is more efficient for single checks, while the iterative approach offers perfect accuracy. Both methods effectively determine if a number is triangular based on the formula T(n) = n × (n + 1) / 2.

Updated on: 2026-03-15T23:19:00+05:30

468 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements