PHP Pronic Number Program
Definition
What Is a Pronic Number?
A pronic number (also called oblong or rectangular) is the product of two consecutive integers:
n = k × (k + 1) for some integer k ≥ 0
Examples: 0, 2, 6, 12, 20, 30, 42, ...
12 = 3 × 4
20 = 4 × 5
| n | Form | Pronic? |
|---|
12 | 3 × 4 | Yes |
0 | 0 × 1 | Yes |
9 | 3 × 3 (not consecutive) | No |
1 | — | No |
Approach
How to Check a Pronic Number in PHP
Four clear steps — then reuse the helper to list pronic numbers in a range.
- If
$n < 0, return false (this tutorial uses non-negative $n). - Start
$k = 0. - While
$k * ($k + 1) ≤ $n, if the product equals $n, return true. - If the product passes
$n with no match, return false.
| Method | Idea | Time | Best for |
|---|
| Loop on k | Try products until > n | O(√n) | Single check (default) |
| Range scan | Call isPronic for each value | O(N√N) | Listing pronic 0..N |
| Generate | Compute k × (k + 1) directly | O(kMax) | Building the sequence |
Pseudocode
function IsPronic(n):
if n < 0: return false
k = 0
while k * (k + 1) <= n:
if k * (k + 1) == n:
return true
k = k + 1
return false
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Definition | n = k × (k + 1) for integer k ≥ 0 |
| Also called | Oblong / rectangular numbers |
| 0 | Pronic (0 × 1) |
| 1 and 9 | Not pronic |
| Evenness | For k ≥ 1, product is always even |
| Classic checks | 12 yes; 20 yes; 9 no |
Try it
Live Preview
Enter an integer to try consecutive products until they pass n.
Trace
Worked Walkthrough — n = 12
Try consecutive products until one matches or passes 12.
k = 0 → 0 × 1 = 0
k = 1 → 1 × 2 = 2
k = 2 → 2 × 3 = 6
k = 3 → 3 × 4 = 12 ✓ match
12 is pronic
Counterexample n = 9:
k = 2 → 6
k = 3 → 12 (> 9) → no match → not pronic
Code
PHP Programs
Start with a reusable isPronic helper, list matches in a range, then generate the sequence by multiplying.
Example 1 — isPronic Check
Return true when some consecutive product equals $n.
<?php
function isPronic(int $n): bool {
if ($n < 0) {
return false;
}
for ($k = 0; $k * ($k + 1) <= $n; $k++) {
if ($k * ($k + 1) === $n) {
return true;
}
}
return false;
}
$number = 12;
if (isPronic($number)) {
echo $number . " is pronic\n";
} else {
echo $number . " is not pronic\n";
}
?>
How It Works
- Negative values are rejected first.
- The loop grows
$k while $k * ($k + 1) stays ≤ $n. - Equality means two consecutive factors were found.
- If the product jumps past
$n, no match exists. - For
12, $k = 3 gives 3 × 4, so the answer is true.
Example 2 — Pronic Numbers in a Range
Reuse isPronic to print every pronic value from 0 to 30.
<?php
function isPronic(int $n): bool {
if ($n < 0) {
return false;
}
for ($k = 0; $k * ($k + 1) <= $n; $k++) {
if ($k * ($k + 1) === $n) {
return true;
}
}
return false;
}
$start = 0;
$end = 30;
echo "Pronic numbers from " . $start . " to " . $end . ":\n";
$line = "";
for ($i = $start; $i <= $end; $i++) {
if (isPronic($i)) {
$line .= $i . " ";
}
}
echo trim($line) . "\n";
?>
Pronic numbers from 0 to 30:
0 2 6 12 20 30
How It Works
- Same
isPronic helper keeps the rule in one place. - The outer loop visits every integer from
$start to $end. - Only values that pass the helper are collected.
0 is included because 0 × 1 = 0.- For larger ranges, generate products directly with
$k++ instead of scanning every $i.
Example 3 — Generate Sequence
Build pronic numbers directly by computing $k * ($k + 1).
<?php
$kMax = 6;
echo "Pronic numbers for k = 0 to " . $kMax . ":\n";
for ($k = 0; $k <= $kMax; $k++) {
$value = $k * ($k + 1);
echo $k . " * " . ($k + 1) . " = " . $value . "\n";
}
?>
Pronic numbers for k = 0 to 6:
0 * 1 = 0
1 * 2 = 2
2 * 3 = 6
3 * 4 = 12
4 * 5 = 20
5 * 6 = 30
6 * 7 = 42
How It Works
$kMax is the largest starting factor you want to print.- Each step multiplies consecutive integers
$k and $k + 1. - No search is needed — every product is pronic by definition.
- Change
$kMax to print a longer list. - This is the fastest way to list many pronic numbers in order.
Edge Cases & Pitfalls
Check these before calling the solution done.
0Zero is pronic
0 × 1 = 0 — do not reject zero by habit.
SquaresNot the same
9 = 3 × 3 is a square, not consecutive factors.
OverflowKeep products safe
For very large $n, watch integer overflow when computing $k * ($k + 1).
NegativesOut of scope
This tutorial treats only non-negative inputs as candidates.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| isPronic (loop on k) | O(√n) | O(1) |
| Pronic in range 0..N | O(N√N) | O(1) |
| Generate sequence | O(kMax) | O(1) |
Because k × (k + 1) ≈ k², the check loop stops near √n. Generating products with $k++ is faster for long ranges.
Remember
Key Takeaways
Definition: n = k × (k + 1) for some integer k ≥ 0.
Check: try products until they pass n.
Facts: 0 is pronic; squares like 9 are not automatically pronic.
Test set: 12 → yes; 20 → yes; 9 → no; 1 → no.
One line: if some consecutive pair multiplies to n, n is pronic.
Frequently Asked Questions
A pronic number equals k × (k + 1) for some integer k ≥ 0. Examples: 0, 2, 6, 12, 20, 30.
Yes. 0 = 0 × 1.
No. No integer k satisfies k × (k + 1) = 1.
Among any two consecutive integers, one is even, so their product is even.
A loop that tries k while k × (k + 1) ≤ n runs in O(√n) time and O(1) extra space.
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