PHP Triangular Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

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.

Remember
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, ...
nSum chainResult
101+2+3+4Triangular
11Triangular
7between 6 and 10Not triangular
151+2+3+4+5Triangular

How to Check a Triangular Number in PHP

Four clear steps — start with the additive loop, then use the formula when generating.

  1. Reject $n < 1 (this page uses positive integers).
  2. Add 1, then 2, then 3, … while the running total is less than $n.
  3. If the total lands exactly on $n, it is triangular.
  4. Or generate terms with $k * ($k + 1) / 2 when you need the sequence.
MethodIdeaTimeBest for
Additive loopSum 1..k until ≥ nO(√n)Interviews, showing the process
Closed formulaT_k = k(k+1)/2O(1) per termGenerating the sequence
Perfect-square test8n+1 is a squareO(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

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Definitionn = 1+2+…+k for some k
FormulaT_k = k(k+1)/2
First values1, 3, 6, 10, 15, 21, 28, 36, 45
Reject$n < 1
Classic yes10 (T_4)
Classic no7
1..501 3 6 10 15 21 28 36 45

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.

Result
n = 10
Chain: 1 → 3 → 6 → 10
T_4 = 4×5/2 = 10
Verdict: TRIANGULAR

Worked Walkthrough — n = 10

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

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.

Example 1 — Check One Number

Keep adding 1, 2, 3, … until the total reaches or passes $n.

php
<?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";
}
?>

How It Works

  1. Reject non-positive inputs immediately.
  2. Start $total = 0 and $k = 1.
  3. While $total < $num, add $k and increment $k.
  4. For 10: totals become 1, 3, 6, then 10.
  5. 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
<?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";
?>

How It Works

  1. Loop $i from 1 to 50 inclusive.
  2. Call isTriangular($i) for each value.
  3. Print only the hits: nine numbers through 45.
  4. This short list is a useful interview sanity check.
  5. 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
<?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";
}
?>

How It Works

  1. For each $k, compute intdiv($k * ($k + 1), 2).
  2. $k * ($k + 1) is always even, so the result stays an exact integer.
  3. Results match the 1..50 list without scanning every integer.
  4. Use this when you need the sequence, not a yes/no check on an arbitrary $n.
  5. 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.

Time and Space Complexity

ProgramTimeExtra space
Additive checkO(√n)O(1)
Range scan of size NO(N √N) naiveO(1)
Generate m termsO(m)O(1)

Because T_k ≈ k²/2, the additive loop needs about √(2n) steps to reach n.

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

A number that equals 1 + 2 + 3 + … + k for some positive integer k. The sequence starts 1, 3, 6, 10, 15, 21, …
It is the closed form of the sum 1+2+…+k. Because k(k+1) is always even, integer division by 2 is exact.
Yes. It is T_1 — a single row with one object.
No. It sits between T_3 = 6 and T_4 = 10.
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.

Next: Abundant Number

Back to the start of the interview list — proper divisors greater than the number.

Abundant number tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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