- Reason
6 = 2*3
Check Pronic Number in Python
What you’ll learn
- What a pronic number is: product of two consecutive integers.
- A simple and safe integer check in Python.
- How to list pronic values in a range with reusable code.
Overview
A number is pronic if it can be written as k*(k+1). We test a single number and then list all pronic numbers in a small range.
Two examples
One program checks one value, another prints all pronic numbers from 1 to 20.
Live preview
Interactive checker shows whether n = k*(k+1) and reports k if found.
Interview clarity
Definition, algorithm, edge cases, complexity, and FAQ in one place.
Prerequisites
Basic Python loops and integer multiplication are enough.
- Write functions using
defand conditionals usingif. - Use
forloops and integer math expressions likek * (k + 1).
What is a pronic number?
A pronic number is any number of the form k*(k+1) where k >= 0.
Examples: 2 = 1*2, 6 = 2*3, 12 = 3*4, 20 = 4*5, and 0 = 0*1.
Why checking up to sqrt(n) works
For n = k*(k+1), k is near sqrt(n). That means only small k values are needed before crossing n.
Try k = 0, 1, 2, 3. At k = 3, 3*(3+1) = 12, so 12 is pronic.
Picture it
Pronic numbers count dots in a rectangle of size k by k+1.
- Reason
9 = 3*3(not consecutive)
Takeaway: factors must be consecutive integers, not just any pair.
Live preview
Try nonnegative integers and see whether the value fits k*(k+1).
Algorithm
Goal: decide if n = k*(k+1) for some integer k >= 0.
Reject negatives
If n < 0, return False.
Try k values
Start from k = 0, compute p = k*(k+1).
Match or stop
If p == n return True, if p > n return False.
📜 Pseudocode
function isPronic(n):
if n < 0:
return false
k = 0
while true:
p = k * (k + 1)
if p == n:
return true
if p > n:
return false
k = k + 1 Check one value
Simple integer loop version. Python integers are arbitrary precision, so no overflow worries for normal interview usage.
def is_pronic(n: int) -> bool:
if n < 0:
return False
k = 0
while True:
product = k * (k + 1)
if product == n:
return True
if product > n:
return False
k += 1
number = 12
if is_pronic(number):
print(f"{number} is a pronic number.")
else:
print(f"{number} is not a pronic number.") Pronic numbers from 1 to 20
Reuses the helper and prints matching values in range.
def is_pronic(n: int) -> bool:
if n < 0:
return False
k = 0
while True:
product = k * (k + 1)
if product == n:
return True
if product > n:
return False
k += 1
print("Pronic numbers in the range 1 to 20:")
for value in range(1, 21):
if is_pronic(value):
print(value, end=" ")
print() Other approaches
Sqrt shortcut. You can check k = int(sqrt(n)) then test k*(k+1) == n.
Quadratic view. From k^2 + k - n = 0, discriminant method is another valid approach.
Interview tip: definition + integer loop is easiest to explain clearly.
❓ FAQ
🔄 Input / output examples
| n | Pronic? | Message |
|---|---|---|
| 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
Pronic if k starts at 0
Because 0 = 0*(0+1).
Not pronic in this definition
Most beginner problems define pronic for nonnegative integers.
Do not confuse with squares
9 is 3*3, not k*(k+1), so it is not pronic.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| Single n (k loop) | O(sqrt(n)) | O(1) |
| Range [a, b] | O((b-a)*sqrt(b)) | O(1) |
| Naive up to n | O(n) | O(1) |
Summary
- Definition: pronic means
n = k*(k+1)for somek >= 0. - Method: loop k and compare
k*(k+1)to n. - Careful: 0 is pronic, negatives are usually excluded, and squares are not automatically pronic.
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
