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.
Add consecutive integers until you reach or pass the target.
Calculation
total = 0, k = 1
+1 → total = 1 (< 10)
+2 → total = 3 (< 10)
+3 → total = 6 (< 10)
+4 → total = 10 (== 10)
Stop
10 is triangular (T_4)
Formula check: 4 × 5 / 2 = 10
Code
Python Programs
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.
python
def is_triangular(num):
if num < 1:
return False
total = 0
k = 1
while total < num:
total += k
k += 1
return total == num
number = 10
if is_triangular(number):
print(f"{number} is a triangular number.")
else:
print(f"{number} is not a triangular number.")
Output
10 is a triangular number.
How It Works
Reject non-positive inputs immediately.
Start total = 0 and k = 1.
While total < num, add k and increment k.
For 10: totals become 1, 3, 6, then 10.
Return whether the final total equals num.
Example 2 — Triangular Numbers from 1 to 50
Reuse the helper to print every triangular value in a small band.
python
def is_triangular(num):
if num < 1:
return False
total = 0
k = 1
while total < num:
total += k
k += 1
return total == num
print("Triangular numbers in the range 1 to 50:")
for i in range(1, 51):
if is_triangular(i):
print(i, end=" ")
print()
Output
Triangular numbers in the range 1 to 50:
1 3 6 10 15 21 28 36 45
How It Works
Loop i from 1 to 50 inclusive with range(1, 51).
Call is_triangular(i) for each value.
Print only the hits: nine numbers through 45.
This short list is a useful interview sanity check.
For longer ranges, generating with the formula is cheaper than testing every i.
Example 3 — Generate with the Closed Formula
Compute Tk = k(k+1)//2 directly for k = 1..9.
python
print("Triangular numbers for k = 1 to 9:")
for k in range(1, 10):
value = k * (k + 1) // 2
print(f"T_{k} = {k}*{k + 1}/2 = {value}")