Home PHP Interview Programs Triangular Number PHP Triangular Number Program Definition
What Is a Triangular Number? A triangular number is a positive integer that equals 1 + 2 + 3 + … + k for some integer k ≥ 1. Equivalently: Tk = k(k+1)/2.
T_k = 1 + 2 + ... + k = k(k+1)/2
Examples:
T_1 = 1
T_2 = 1+2 = 3
T_3 = 1+2+3 = 6
T_4 = 1+2+3+4 = 10
Sequence: 1, 3, 6, 10, 15, 21, 28, 36, 45, ... n Sum chain Result 101+2+3+4 Triangular 11 Triangular 7between 6 and 10 Not triangular 151+2+3+4+5 Triangular
Approach
How to Check a Triangular Number in PHP 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)/2O(1) per termGenerating the sequence Perfect-square test 8n+1 is a squareO(1)Optional fast check
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 kFormula T_k = k(k+1)/2First values 1, 3, 6, 10, 15, 21, 28, 36, 45 Reject $n < 1Classic yes 10 (T_4)Classic no 71..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.
Trace
Worked Walkthrough — n = 10 Add consecutive integers until you reach or pass the target.
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
PHP 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.
1. Check one number 2. Range 1 to 50 3. Generate Tk Example 1 — Check One Number Keep adding 1, 2, 3, … until the total reaches or passes $n.
<?php
function isTriangular($num) {
if ($num < 1) {
return false;
}
$total = 0;
$k = 1;
while ($total < $num) {
$total += $k;
$k++;
}
return $total === $num;
}
$number = 10;
if (isTriangular($number)) {
echo $number . " is a triangular number.\n";
} else {
echo $number . " is not a triangular number.\n";
}
?> 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.
<?php
function isTriangular($num) {
if ($num < 1) {
return false;
}
$total = 0;
$k = 1;
while ($total < $num) {
$total += $k;
$k++;
}
return $total === $num;
}
echo "Triangular numbers in the range 1 to 50:\n";
$line = "";
for ($i = 1; $i <= 50; $i++) {
if (isTriangular($i)) {
$line .= $i . " ";
}
}
echo trim($line) . "\n";
?> 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. Call isTriangular($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.
<?php
echo "Triangular numbers for k = 1 to 9:\n";
for ($k = 1; $k <= 9; $k++) {
$value = intdiv($k * ($k + 1), 2);
echo "T_" . $k . " = " . $k . "*" . ($k + 1) . "/2 = " . $value . "\n";
}
?> Triangular numbers for k = 1 to 9:
T_1 = 1*2/2 = 1
T_2 = 2*3/2 = 3
T_3 = 3*4/2 = 6
T_4 = 4*5/2 = 10
T_5 = 5*6/2 = 15
T_6 = 6*7/2 = 21
T_7 = 7*8/2 = 28
T_8 = 8*9/2 = 36
T_9 = 9*10/2 = 45 How It Works For each $k, compute intdiv($k * ($k + 1), 2). $k * ($k + 1) is always even, so the result stays an exact integer.Results match the 1..50 list without scanning every integer. Use this when you need the sequence, not a yes/no check on an arbitrary $n. In interviews, mention the formula after showing the additive loop. Edge Cases & Pitfalls Check these before calling the solution done.
n < 1 Not triangular here Zero and negatives are rejected under the positive-integer definition.
Between Overshoot means no If the sum jumps past $n (like 7 between 6 and 10), return false.
Formula Multiply before divide Prefer intdiv($k * ($k + 1), 2). Multiplying first keeps the product even before dividing.
Overflow Huge k Very large products can exceed int range — keep classroom examples modest.
Analysis
Time and Space Complexity Program Time Extra space Additive check O(√n)O(1)Range scan of size N O(N √N) naiveO(1)Generate m terms O(m)O(1)
Because T_k ≈ k²/2, the additive loop needs about √(2n) steps to reach n.
Remember
Key Takeaways Definition: $n = 1+2+…+k for some $k ≥ 1.
Loop: add consecutive integers until you reach or pass $n.
Formula: T_k = k(k+1)/2.
Test set: 1, 10, 15 yes; 7 no; 1..50 → 1 3 6 10 15 21 28 36 45.
One line: keep adding 1, 2, 3, … — if you land exactly on $n, it is triangular.
Frequently Asked Questions What is a triangular number? A number that equals 1 + 2 + 3 + … + k for some positive integer k. The sequence starts 1, 3, 6, 10, 15, 21, …
What is the formula T_k = k(k+1)/2? It is the closed form of the sum 1+2+…+k. Because k(k+1) is always even, integer division by 2 is exact.
Is 1 triangular? Yes. It is T_1 — a single row with one object.
Is 7 triangular? No. It sits between T_3 = 6 and T_4 = 10.
What is the time complexity? The additive check is O(√n). Generating with the formula is O(1) per term, or O(m) to print the first m triangular numbers.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion