Check if Number is Prime in DSA C - Time & Space Complexity
We want to know how the time to check if a number is prime grows as the number gets bigger.
How many steps does it take to decide if a number has no divisors other than 1 and itself?
Analyze the time complexity of the following code snippet.
#include <stdbool.h>
#include <math.h>
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= (int)sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
This code checks if a number is prime by testing divisors from 2 up to the square root of the number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that tests divisors from 2 to sqrt(n).
- How many times: Approximately sqrt(n) times in the worst case.
As the number n grows, the number of divisor checks grows roughly with the square root of n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 checks (2,3) |
| 100 | About 10 checks (2 to 10) |
| 1000 | About 31 checks (2 to 31) |
Pattern observation: The number of steps grows slower than n, closer to the square root of n.
Time Complexity: O(√n)
This means the time to check if a number is prime grows roughly with the square root of the number.
[X] Wrong: "We must check all numbers from 2 up to n-1 to find if n is prime."
[OK] Correct: Checking beyond the square root is unnecessary because any larger factor pairs with a smaller one already checked.
Understanding this time complexity helps you explain efficient prime checks and shows you can optimize simple loops, a useful skill in many coding problems.
"What if we checked divisors only up to n/2 instead of sqrt(n)? How would the time complexity change?"
Practice
prime?Solution
Step 1: Understand the definition of prime number
A prime number is a number greater than 1 that has no divisors other than 1 and itself.Step 2: Compare options with definition
Only It has exactly two distinct positive divisors: 1 and itself. correctly states this definition. Options B, C, and D are incorrect because they describe properties that do not define prime numbers.Final Answer:
It has exactly two distinct positive divisors: 1 and itself. -> Option BQuick Check:
Prime number = two divisors [OK]
- Thinking all even numbers are prime
- Confusing prime with composite numbers
- Including 1 as a prime number
n is prime?Solution
Step 1: Analyze loop range for divisor checking
To check ifnis prime, we test divisors from 2 up ton-1. for(int i = 2; i < n; i++) { if(n % i == 0) return 0; } return 1; usesi < n, which is correct.Step 2: Check return values and conditions
for(int i = 2; i < n; i++) { if(n % i == 0) return 0; } return 1; returns 0 if a divisor is found, else returns 1 meaning prime. Other options have incorrect loop ranges or return values.Final Answer:
for(int i = 2; i < n; i++) { if(n % i == 0) return 0; } return 1; -> Option CQuick Check:
Loop from 2 to n-1 and return 0 if divisible [OK]
- Checking divisibility by 1
- Looping up to n instead of n-1
- Returning wrong values on divisor found
n = 11?
int isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i < i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
printf("%d", isPrime(n));Solution
Step 1: Analyze the for loop condition
In C, relational operators (<, <=) associate left-to-right with same precedence.i < i * i <= nparses as(i < (i * i)) <= n. Fori ≥ 2,i < i*iis true (1), so1 <= nis true. Condition always true.Step 2: Runtime behavior
Loop never terminates (infinite loop). No printf output.Final Answer:
Infinite loop -> Option DQuick Check:
(i < i*i) <= n always true [OK]
- Thinking chained comparisons cause compile error
- Assuming Python-like chained comparison
- Believing loop stops at sqrt(n)
n is prime:
int isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i = 0) return 0;
}
return 1;
}Solution
Step 1: Check the if condition inside the loop
The condition usesn % i = 0which is assignment, not comparison.Step 2: Understand impact of assignment in condition
This causes a syntax error or unintended behavior. It should ben % i == 0to compare.Final Answer:
Using assignment operator '=' instead of comparison '==' in if condition -> Option AQuick Check:
Use '==' for comparison, not '=' [OK]
- Confusing '=' and '==' in conditions
- Looping till n/2 is actually correct optimization
- Ignoring compiler error messages
n. Which change improves efficiency the most?
int isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) return 0;
}
return 1;
}Solution
Step 1: Understand loop optimization for prime checking
Checking divisors up ton/2is inefficient. We only need to check up to the square root ofn.Step 2: Apply loop condition using
Changing the loop to run whilei * i <= ni * i <= nreduces iterations and improves efficiency.Final Answer:
Change loop to run till i * i <= n instead of i <= n / 2 -> Option AQuick Check:
Check divisors up to sqrt(n) for efficiency [OK]
- Checking even numbers only misses odd divisors
- Starting loop from 1 wastes time
- Removing base case causes wrong results
Ready to prove it?
Pass the test and this pattern moves unknown
