Skip to content

Commit 2334fae

Browse files
committed
feat: Add PrimeOrNot class with isPrime method for prime number check
WHAT the code does: - Defines a `PrimeOrNot` class with a static method `isPrime(int x)`. - `isPrime()` checks if a number is prime: - Returns `false` for numbers ≤ 1. - Loops from `2` to `x/2` checking divisibility. - Returns `false` if any divisor is found, otherwise `true`. - `main()` tests the method with numbers: 13, 91, 100, 2, and 1. WHY this matters: - Demonstrates prime number validation using basic loops and conditions. - Reinforces modular arithmetic (`%`) and control flow. - A classic programming exercise useful in interviews and algorithm practice. HOW it works: 1. Input `x = 13` → loop finds no divisor → returns `true`. 2. Input `x = 91` → divisible by 7 → returns `false`. 3. Input `x = 100` → divisible by 2 → returns `false`. 4. Input `x = 2` → no divisors found → returns `true`. 5. Input `x = 1` → `<= 1` check → returns `false`. Tips & gotchas: - The loop `i <= x/2` works but is inefficient for large numbers → can be optimized to `i <= Math.sqrt(x)`. - Handles negative numbers by treating them as not prime. - For efficiency, you could check divisibility by 2 first, then loop odd numbers only. - Returning early on divisor detection keeps it performant. Use-cases: - Educational example for prime checking. - Useful in math-heavy applications or competitive programming. - Can serve as a utility function in algorithms needing prime detection. - Good base for extending to prime factorization. Short key: algo-prime-check-basic. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 1ff2454 commit 2334fae

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed
Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
public class PrimeOrNot {
2-
public static void main(String[] args)
3-
{
2+
public static void main(String[] args) {
43
System.out.println(isPrime(13));
54
System.out.println(isPrime(91));
65
System.out.println(isPrime(100));
6+
System.out.println(isPrime(2));
7+
System.out.println(isPrime(1));
78
}
89

9-
public static boolean isPrime(int x)
10-
{
11-
//13 --> 1,2,3,4,5,6,7,8,9,10,11,12,13
12-
int res = 0;
13-
for (int i = 1; i < x / 2; i++)
10+
/*
11+
Declaring a method 'isPrime'. and call this method into thr main method.
12+
13+
A prime number is a number greater than 1 that has exactly two factors: 1 & itself.
14+
Examples:
15+
2 → factors: 1, 2 → prime ✅
16+
3 → factors: 1, 3 → prime ✅
17+
4 → factors: 1, 2, 4 → not prime ❌(extra factor)
18+
13 → factors: 1, 13 → prime ✅
19+
*/
20+
21+
public static boolean isPrime(int x) {
22+
if (x <= 1) return false; // 0 and 1 are not prime.
23+
24+
for (int i = 2; i <= x / 2; i++)
1425
{
15-
if (x % i == 0)
16-
{
17-
res++;
26+
if (x % i == 0) {
27+
return false; // found a divisor → not prime.
1828
}
1929
}
20-
return res == 1;
30+
return true; // no divisors found → prime
2131
}
22-
}
32+
}
33+
34+
/*
35+
i <= x / 2; → keep looping until half of the number.
36+
Why? Because a number can’t have a factor greater than x/2 (except itself).
37+
*/

0 commit comments

Comments
 (0)