Check Pronic Number in JavaScript

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • What a pronic number is (product of two consecutive integers).
  • A safe integer test in JavaScript without relying only on sqrt rounding.
  • How to list pronic values in a range (same output style as the classic demo: 2 6 12 20 up 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.log in JavaScript.
  • Optional: long long for the product k * (k+1) when you want extra safety on large n.

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.

Pronic n = k(k+1)
Square n = k²
Neither e.g. 1, 3, 5

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.

Worked check: n = 12

k = 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.

6 Pronic
Factorization
2 × 3 (consecutive)
9 Not pronic
Why
3 × 3 uses 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.

Use a nonnegative integer. Very large values may be rejected in this demo.

Live result
Press “Run check”.

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

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 + 1
1

Check 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.

JavaScript
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.");
}
Try it Yourself

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.

2

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.

JavaScript
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(" "));
Try it Yourself

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

Take two counting numbers that come one after the other, like 3 and 4. Multiply them: 3 times 4 equals 12. That product is a pronic number. So a pronic number is always k times k+1 for some k (often k is allowed to be zero, giving 0 times 1 equals 0).
No. There is no integer k with k times k+1 equal to 1. The pronic numbers start 0, 2, 6, 12, 20, and so on.
Yes. Use k equals 3: 3 times 4 equals 12.
For exact math, if n equals k times k+1 then k is floor(sqrt(n)). But floating-point square root can round wrong for very large integers, so a small integer loop is safer in real code.
Either k or k+1 is even, so k times k+1 is always even when k is at least 1. The value 0 is also pronic (k equals 0).
Twice a triangular number t equals k times k+1, so pronic numbers are exactly double the triangular numbers 0, 1, 3, 6, 10, and so on.

🔄 Input / output examples

Swap the literal in script variables, or read input with prompt / readline for interactive runs.

nPronic?One-line message (Example 1 style)
12Yes (3×4)12 is a pronic number.
9No9 is not a pronic number.
0Yes (0×1)0 is a pronic number.
2Yes (1×2)2 is a pronic number.

Edge cases and pitfalls

Watch types, signs, and the difference between “close” factors and consecutive factors.

Overflow

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.

sqrt

Floating-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.

Range

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

TaskTimeExtra space
Single n with k loopO(√n)O(1)
Scan [a, b]O((b-a)√b) in the worst caseO(1)
Naive inner loop 0..nO(n) per checkO(1)

No arrays are required; memory use stays constant beyond the program stack.

Summary

  • Definition: n is pronic when n = k(k+1) for some k ≥ 0.
  • Code: try k in order with a wide product type; stop when you match or overshoot n.
  • Watch-outs: do not confuse squares with pronic; mind overflow; treat sqrt shortcuts with care on big integers.
Did you know?

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, …

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.

9 people found this page helpful