- Factorization
2 × 3(consecutive)
Check Pronic Number in JavaScript
What you’ll learn
- What a pronic number is (product of two consecutive integers).
- A safe integer test in JavaScript without relying only on
sqrtrounding. - How to list pronic values in a range (same output style as the classic demo:
2 6 12 20up to 20).
Overview
Pick any nonnegative integer k. Multiply k by the next integer k + 1. That product k(k+1) is a pronic number. Your program checks whether a given n equals such a product, then can print every pronic value in a short interval.
Two programs
One single-value check (sample 12) and one range listing (1–20).
Live preview
Type n and see if n = k(k+1) for some k, with k shown when it works.
Clearer than sqrt-only demos
Some older snippets use sqrt on int without mentioning rounding; here the main example stays in integer arithmetic.
Prerequisites
Comfortable with for loops, if, and integer multiplication.
- Functions, loops, and
console.login JavaScript. - Optional:
long longfor the productk * (k+1)when you want extra safety on largen.
What is a pronic number?
A pronic number is any value you can write as k × (k + 1) where k is a nonnegative integer. Names you might hear: oblong, rectangular, or heteromecic—they all mean the same thing here.
Examples: 1 × 2 = 2, 2 × 3 = 6, 3 × 4 = 12, 4 × 5 = 20. If you include k = 0, you get 0 × 1 = 0, which is also pronic.
Why we only try k up to about √n
If n = k(k+1) and k ≥ 1, then k < k+1, so k2 < k(k+1) = n < (k+1)2. Taking square roots gives k < √n < k+1, so k is exactly ⌊√n⌋ in exact arithmetic. That is why a short loop over k stops once k(k+1) > n—you only need k up to roughly √n.
n = 12k = 0: 0 × 1 = 0. k = 1: 2. k = 2: 6. k = 3: 3 × 4 = 12 — match, so 12 is pronic.
Picture it
Imagine a grid of dots with k rows and k+1 columns (or the other way around). The total count is k(k+1)—that is the “rectangle” name.
- Why
3 × 3uses the same factor twice, not consecutive integers.
Takeaway: pronic means “two neighbors on the number line multiplied together,” not “any two factors.”
Live preview
Tries k = 0, 1, 2, … until k(k+1) reaches or passes n. Large inputs are capped so the page stays responsive.
Algorithm
Goal: decide whether n can be written as k(k+1) for some integer k ≥ 0.
Optional: reject negatives
Most assignments use n ≥ 0. If n < 0, return “not pronic” (or handle as invalid input).
Enumerate k
Start at k = 0. Compute p = k(k+1) using a wide enough type if needed. If p == n, success. If p > n, stop and report failure.
Range listing
For each n in [low, high], run the same test and print n when the test succeeds.
📜 Pseudocode
function isPronic(n):
if n < 0:
return false
k ← 0
loop:
p ← k * (k + 1)
if p = n:
return true
if p > n:
return false
k ← k + 1Check one value (integer loop, wide product)
Uses long long for k * (k + 1) so the product does not overflow a 32-bit int while still comparing to n. Sample value 12 matches the classic walkthrough.
function isPronic(n) {
if (n < 0) {
return false;
}
for (let k = 0; ; k++) {
const p = k * (k + 1);
if (p === n) {
return true;
}
if (p > n) {
return false;
}
}
}
const number = 12;
if (isPronic(number)) {
console.log(number + " is a pronic number.");
} else {
console.log(number + " is not a pronic number.");
}Explanation
The loop is the definition turned into code: try each k in order until the product equals n or jumps past it.
long long p = k * (k + 1);Room to multiply. For larger k, k(k+1) can exceed two billion, so a wider type keeps behavior predictable.
if (p > n) return 0;Stop early. The sequence k(k+1) grows forever, so once it passes n, equality will never happen for a larger k.
Pronic numbers from 1 to 20
Reuses is_pronic. The inner test runs in O(√n) time per value, unlike a naive inner loop that walks i all the way to n.
function isPronic(n) {
if (n < 0) {
return false;
}
for (let k = 0; ; k++) {
const p = k * (k + 1);
if (p === n) {
return true;
}
if (p > n) {
return false;
}
}
}
const values = [];
for (let i = 1; i <= 20; i++) {
if (isPronic(i)) {
values.push(String(i));
}
}
console.log("Pronic numbers in the range 1 to 20:");
console.log(values.join(" "));Explanation
The outer loop is just “try every n in the assignment range.” All the math lives inside is_pronic.
for (int i = 1; i <= 20; ++i)Matches the classic demo. Pronic values here are 2, 6, 12, 20; 0 is pronic too but sits outside 1…20.
Other approaches
Square-root shortcut (careful). In exact arithmetic, if n = k(k+1) then k = ⌊√n⌋. Some programs use Math.floor(Math.sqrt(n)) and test r * (r + 1) === n. That is short but can mis-classify very large integers because of floating-point rounding; prefer integers for grading-safe code.
Quadratic formula. k(k+1) = n gives k = (-1 + √(1+4n)) / 2; again, prefer an integer discriminant check if you use this route.
Interview: say the definition, then give the k loop or the discriminant test with integer checks.
❓ FAQ
🔄 Input / output examples
Swap the literal in script variables, or read input with prompt / readline for interactive runs.
n | Pronic? | One-line message (Example 1 style) |
|---|---|---|
12 | Yes (3×4) | 12 is a pronic number. |
9 | No | 9 is not a pronic number. |
0 | Yes (0×1) | 0 is a pronic number. |
2 | Yes (1×2) | 2 is a pronic number. |
Edge cases and pitfalls
Watch types, signs, and the difference between “close” factors and consecutive factors.
k * (k + 1) in plain int
For big k, the product can overflow before you compare to n. Using long long for p (or widening n) avoids silent wrong answers.
sqrtFloating-point
A one-liner with math.h looks elegant but is not the most robust way to classify huge integers; examiners like hearing that trade-off.
Including zero
If your range starts at 1, you will never print 0 even though 0 is pronic. That is expected for the 1…20 demo.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Single n with k loop | O(√n) | O(1) |
Scan [a, b] | O((b-a)√b) in the worst case | O(1) |
Naive inner loop 0..n | O(n) per check | O(1) |
No arrays are required; memory use stays constant beyond the program stack.
Summary
- Definition:
nis pronic whenn = k(k+1)for somek ≥ 0. - Code: try
kin order with a wide product type; stop when you match or overshootn. - Watch-outs: do not confuse squares with pronic; mind overflow; treat
sqrtshortcuts with care on big integers.
Pronic numbers are also called oblong or rectangular because k(k+1) counts dots in a rectangle with sides k and k+1. The sequence starts 0, 2, 6, 12, 20, 30, …
9 people found this page helpful
