- Because
- 3 x 4
Check Pronic Number in Java
What you’ll learn
- What a pronic number is:
k * (k + 1). - How to test by increasing
kuntil match or overshoot. - Two Java programs: one-number check and range listing.
- Live preview behavior, edge cases, and complexity.
Overview
A pronic number is the product of two consecutive integers. Examples are 0, 2, 6, 12, 20, 30, formed by k * (k + 1) for k = 0, 1, 2, 3, 4, 5.
Consecutive product
Pattern is always k and k + 1.
Live test
Try 12 (yes) and 9 (no).
Loop stop rule
Stop when k*(k+1) exceeds n.
Prerequisites
Loops, multiplication, and simple integer comparisons.
- Understand expression
k * (k + 1). - Use
longin checks to avoid multiplication overflow.
What is a pronic number?
A number is pronic if it can be expressed as k * (k + 1) for integer k >= 0.
0 is pronic because 0 = 0 * 1. 1 is not pronic.
Math note
Sequence grows roughly like squares, so number of checks is about sqrt(n).
Scan k upward and stop once k*(k+1) > n.
Quick examples
- Reason
- No consecutive pair product
- Because
- 0 x 1
Live preview
Checks whether a value equals k*(k+1) for some nonnegative integer k.
Algorithm
Goal: decide if integer n is pronic.
Reject negatives
For this tutorial, pronic values are from k >= 0.
Scan k
Compute p = k*(k+1) for increasing k.
Match or overshoot
If p == n, true; if p > n, false.
📜 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 number
public class Main {
static boolean isPronic(int n) {
if (n < 0) return false;
for (long k = 0; ; k++) {
long p = k * (k + 1);
if (p == n) return true;
if (p > n) return false;
}
}
public static void main(String[] args) {
int number = 12;
if (isPronic(number)) {
System.out.println(number + " is a pronic number.");
} else {
System.out.println(number + " is not a pronic number.");
}
}
}Pronic numbers in range 1 to 20
public class Main {
static boolean isPronic(int n) {
if (n < 0) return false;
for (long k = 0; ; k++) {
long p = k * (k + 1);
if (p == n) return true;
if (p > n) return false;
}
}
public static void main(String[] args) {
System.out.println("Pronic numbers in the range 1 to 20:");
for (int i = 1; i <= 20; i++) {
if (isPronic(i)) {
System.out.print(i + " ");
}
}
}
}Optimization notes
Stop checking once k*(k+1) > n; this yields roughly O(sqrt(n)) checks.
❓ FAQ
🔄 Input / output examples
| Input | Pronic? |
|---|---|
12 | Yes |
9 | No |
Edge cases
0 = 0*1, so zero is pronic.
Treat negatives as non-pronic for this tutorial.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| Single pronic check | O(√n) | O(1) |
| Check all in range 1..U | about O(U√U) | O(1) |
Summary
- Pronic numbers are products of consecutive integers.
- Check by scanning
kuntil match or overshoot.
Pronic numbers are products of two consecutive integers: 0, 2, 6, 12, 20, 30...
9 people found this page helpful
