- Because
- 3 × 4
Check Pronic Number in C#
What you’ll learn
- What a pronic number is:
k * (k + 1)for nonnegativek. - How to test by increasing
kuntil you match or overshoot. - Two C# programs: one-number check and pronic listing in a range.
- 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 the expression
k * (k + 1). - Use
longin checks to avoid multiplication overflow.
Understanding the concept of pronic numbers
A number is pronic if it can be expressed as k * (k + 1) for integer k >= 0. Also called oblong, rectangular, or heteromecic numbers.
Pn = n × (n + 1) — for example P3 = 3 × 4 = 12. 0 is pronic because 0 = 0 × 1. 1 is not pronic.
Math note
The pronic sequence grows roughly like squares, so the number of k values to try is about sqrt(n).
Scan k upward and stop once k * (k + 1) > n.
Quick examples
- Reason
- No consecutive pair product
- Because
- 0 × 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 come from k >= 0.
Scan k
Compute p = k * (k + 1) for increasing k.
Match or overshoot
If p == n, return true; if p > n, return 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 + 1 Check one number
Reference program: tests whether 12 is pronic using a safe long k-loop.
using System;
class Program
{
static bool IsPronic(int num)
{
if (num < 0)
return false;
for (long k = 0; ; k++)
{
long product = k * (k + 1);
if (product == num)
return true;
if (product > num)
return false;
}
}
static void Main()
{
int number = 12;
if (IsPronic(number))
Console.WriteLine($"{number} is a pronic number.");
else
Console.WriteLine($"{number} is not a pronic number.");
}
} Pronic numbers in range 1 to 20
Reuses IsPronic inside a range loop — matches the reference range output.
using System;
class Program
{
static bool IsPronic(int num)
{
if (num < 0)
return false;
for (long k = 0; ; k++)
{
long product = k * (k + 1);
if (product == num)
return true;
if (product > num)
return false;
}
}
static void Main()
{
Console.WriteLine("Pronic Numbers in the range 1 to 20:");
for (int i = 1; i <= 20; i++)
{
if (IsPronic(i))
Console.Write($"{i} ");
}
Console.WriteLine();
}
} Optimization and alternatives
k-loop (preferred). Stop once k * (k + 1) > n — about O(sqrt(n)) checks.
Sqrt shortcut. For small n, int k = (int)Math.Sqrt(num); return k * (k + 1) == num; works, but floating-point rounding makes the integer loop safer.
Bounded for-loop. for (int i = 0; i * (i + 1) <= num; i++) also works when products stay within range.
❓ FAQ
🔄 Input / output examples
| Input | Pronic? | Because |
|---|---|---|
12 | Yes | 3 × 4 |
9 | No | No consecutive pair |
0 | Yes | 0 × 1 |
1 | No | Not a product k(k+1) |
Edge cases and pitfalls
n = 0
0 = 0 × 1, so zero is pronic.
n = 1
Not pronic — the sequence after 0 is 2, 6, 12…
Non-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
kandk + 1. 12is pronic because3 × 4 = 12.- From
1to20, pronic numbers are2 6 12 20.
Pronic numbers are products of two consecutive integers: 0, 2, 6, 12, 20, 30… They are also called oblong or rectangular numbers.
9 people found this page helpful
