Add Rows
1+2+3+…
Each total after a full row is triangular.
A triangular number is the total of stacked rows: 1 + 2 + 3 + ... + k, also written Tk = k(k+1)/2. Examples: 1, 3, 6, 10, 15. Non-example: 7 (between 6 and 10). This tutorial covers an additive loop check, a live preview, worked Python examples, edge cases, and complexity.
1+2+3+…
Each total after a full row is triangular.
total == n
Overshooting means not triangular.
k(k+1)/2
Closed form for the same sum.
First T1
A single-dot triangle counts.
Try 10 / 7
Watch the running total grow.
1 3 6 … 45
See the sequence at a glance.
A triangular number is the total after stacking rows of size 1, then 2, then 3, and so on. So 10 = 1+2+3+4 is triangular, while 7 is not — it falls between T3 = 6 and T4 = 10.
Interviews love the additive loop because you can narrate each row. The closed form Tk = k(k+1)/2 (and the related 8n+1 perfect-square test) are useful shortcuts once the idea is clear.
It connects geometry (stacked rows) to loops and formulas — a friendly number-theory warm-up after basics like swap.
1 + 2 + … + k
Stop at or past n.
k(k+1)/2
Twice a triangular.
In short: add 1, then 2, then 3… until you hit n exactly or pass it.
Given a positive integer n, decide whether it equals 1+2+…+k for some k >= 1.
# 10 -> 1+2+3+4 = 10 triangular
# 7 -> between 6 and 10 not triangular
# 1 -> 1 triangular
# 6 -> 1+2+3 = 6 triangular | Item | Type | Description |
|---|---|---|
num | int | Value to test (n >= 1 on this page). |
| Return | bool | True when some k has T_k = num. |
total / k | int | Running sum and next row size. |
function is_triangular(num):
if num < 1:
return false
total ← 0
k ← 1
while total < num:
total ← total + k
k ← k + 1
return total = num | Method | Idea | Notes |
|---|---|---|
| Additive loop | total += k until >= n | Interview / classroom default |
| Generate | print k*(k+1)//2 | Best for listing the sequence |
| 8n+1 square | math.isqrt(8*n+1) | Fast contest shortcut |
| Goal | Pattern |
|---|---|
| Reject non-positive | if num < 1: return False |
| Add next row | total += k; k += 1 |
| Stop | while total < num: |
| Verdict | return total == num |
| Closed form | k * (k + 1) // 2 |
| Fast check | 8*num + 1 is an odd perfect square |
Same sequence — different packaging.
total += kClearest check for one n
k*(k+1)//2Build the sequence directly
isqrtContest shortcut
2 * T_kPronic = twice triangular
Reach for a triangular check when row-sums or handshake-style totals appear.
Loop + exact-match narrative.
Print T_k values in a band.
Bowling pins, stacked dots.
n people → T_(n-1) handshakes.
Different object; related name only.
Key benefit: one picture — stacked rows — that maps cleanly to a short loop and a closed formula.
Adds 1, then 2, then 3… until the running total reaches or passes your target.
Three complete Python programs — check 10, list triangular numbers from 1 to 50, and generate with the closed formula. Click View Output to reveal sample console results.
A pure additive loop with no imports.
Beginner-friendly loop. Change number (or use input()) to test other values.
def is_triangular(num: int) -> bool:
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.") For 10 the loop adds 1 + 2 + 3 + 4. The total lands exactly on 10, so the function returns True.
Reuse the helper to list nearby triangular values.
The same test runs inside a loop so you can see every triangular value in a small window.
def is_triangular(num: int) -> bool:
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() Within 1..50 the hits are 1, 3, 6, 10, 15, 21, 28, 36, and 45. Memorizing this short list is a useful interview sanity check.
When you only need the sequence, compute k*(k+1)//2 directly instead of testing every integer.
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}") Integer division with // keeps results exact because k(k+1) is always even. This matches the 1..50 list without scanning every i.
This page uses positive counting numbers.
total += k; k += 1
Avoid an infinite loop on misses.
Exact hit means triangular.
Compare an exact hit with a miss that overshoots.
| Add | total | vs 10 | vs 7 |
|---|---|---|---|
+1 | 1 | < | < |
+2 | 3 | < | < |
+3 | 6 | < | < |
+4 | 10 | = yes | > no (passed 7) |
10 lands exactly; 7 is skipped between 6 and 10.
Where triangular checks show up beyond the interview prompt.
Additive loop + exact match.
Example: is_triangular(10).
Find T_k values in a band.
Example: 1 3 6 … 45.
Build with k(k+1)//2.
Example: Example 3.
Pairs among n people.
Example: T_(n-1).
Pronic = 2 × triangular.
Example: related FAQ.
Visual triangles in print loops.
Example: related CTA.
Pro Tip: open with “1+2+…+k” before naming the formula.
Why the additive loop works well for beginners and interviews.
Dry-run 10 as 1+2+3+4 out loud.
Pure arithmetic and a while loop.
Stop as soon as total passes n.
Same idea as k(k+1)//2 and 8n+1.
Pro Tip: lead with the loop; mention the 8n+1 square test only as an optional optimization.
Small habits that keep triangular solutions interview-ready.
Return False for this tutorial’s definition.
while total < num prevents infinite loops.
total == num, not just >=.
Use k*(k+1)//2 for long ranges.
1 3 6 10 15 21 28 36 45
Pro Tip: sanity-check 1, 7, 10, and 15 — if those four behave, your logic is solid.
Mistakes that commonly break triangular-number programs.
Only checking equality inside the loop.
→ Loop while total < num.
Returning True when total > num.
→ Require total == num.
Outside this page’s positive definition.
→ Reject num < 1 (unless class defines T_0).
Different triangle concept.
→ Clarify: row-sum sequence only.
Using / instead of // for k(k+1)/2.
→ Prefer // so results stay ints.
Handle these before claiming the check is complete.
This page uses 1, 2, 3… (some books define T_0 = 0).
Smallest positive triangular number.
Between 6 and 10.
Loop while total < num.
1+2+3+4 = 10.
Use k*(k+1)//2.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
Quick Takeaway: n is triangular when some k >= 1 satisfies 1+2+…+k == n.
| Approach | Time | Extra space |
|---|---|---|
| Additive loop | O(sqrt(num)) | O(1) |
| Generate first m values | O(m) | O(1) |
| Scan 1..U with per-value test | roughly O(U^(3/2)) | O(1) |
Because T_k grows like k²/2, the additive search stops after about sqrt(2n) steps.
A triangular number is the sum of the first k counting numbers. Add rows until you hit n exactly or pass it, remember that 1 is yes and 7 is no, and upgrade to k(k+1)//2 when you need to generate the sequence.
Practice the three examples above, then explore star-pattern programs for printed triangles.
1+2+…+k equals n means triangular.
Classify row-sum totals the interview-friendly way.
1+2+…+k
Definitionadd until
Methodhit or miss
Verdictk(k+1)/2
ShortcutO(√n)
AnalysisThe nth triangular number counts how many balls you need to make a tight triangle with n rows: 1 in the top row, 2 in the next, then 3, and so on. The sequence starts 1, 3, 6, 10, 15, 21... and shows up in handshakes, bowling pins, and simple loop puzzles.
Practice printing triangular shapes with nested loops in Python.
9 people found this page helpful