Four clear steps — start with the additive loop, then use the formula when generating.
Reject n < 1 (this page uses positive integers).
Add 1, then 2, then 3, … while the running total is less than n.
If the total lands exactly on n, it is triangular.
Or generate terms with k * (k + 1) / 2 when you need the sequence.
Method
Idea
Time
Best for
Additive loop
Sum 1..k until ≥ n
O(√n)
Interviews, showing the process
Closed formula
T_k = k(k+1)/2
O(1) per term
Generating the sequence
Perfect-square test
8n+1 is a square
O(1)
Optional fast check
Pseudocode
Pseudocode
function IsTriangular(n):
if n < 1:
return false
total = 0
k = 1
while total < n:
total = total + k
k = k + 1
return total == n
function T(k):
return k * (k + 1) / 2
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Definition
n = 1+2+…+k for some k
Formula
T_k = k(k+1)/2
First values
1, 3, 6, 10, 15, 21, 28, 36, 45
Reject
n < 1
Classic yes
10 (T_4)
Classic no
7
1..50
1 3 6 10 15 21 28 36 45
Try it
Live Preview
Enter a positive integer to see the additive chain and the triangular verdict.
Positive whole numbers. Tap a chip or type a value.
Start with the additive check so you can show each sum. Then list a range, and finish by generating terms with the closed formula.
Example 1 — Check One Number
Keep adding 1, 2, 3, … until the total reaches or passes n.
JavaScript
function isTriangular(num) {
if (num < 1) {
return false;
}
let total = 0;
let k = 1;
while (total < num) {
total += k;
k++;
}
return total === num;
}
const number = 10;
if (isTriangular(number)) {
console.log(number + " is a triangular number.");
} else {
console.log(number + " is not a triangular number.");
}
Reuse the helper to print every triangular value in a small band.
JavaScript
function isTriangular(num) {
if (num < 1) {
return false;
}
let total = 0;
let k = 1;
while (total < num) {
total += k;
k++;
}
return total === num;
}
console.log("Triangular numbers in the range 1 to 50:");
let line = "";
for (let i = 1; i <= 50; i++) {
if (isTriangular(i)) {
line += i + " ";
}
}
console.log(line.trim());