Check if Number is Prime in DSA C++ - Time & Space Complexity
We want to understand how long it takes to check if a number is prime as the number gets bigger.
How does the time needed grow when the input number increases?
Analyze the time complexity of the following code snippet.
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= 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: Loop checking divisors from 2 to √n.
- How many times: Approximately √n times in the worst case.
As the number gets bigger, the loop runs more times but only up to its square root.
| 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 checks grows roughly with the square root of the input number.
Time Complexity: O(√n)
This means the time to check if a number is prime grows slowly, only as the square root of the number.
[X] Wrong: "We need to check all numbers up to n-1 to find if n is prime."
[OK] Correct: Checking up to n-1 is unnecessary because any factor larger than √n pairs with a smaller factor already checked.
Understanding this time complexity helps you explain efficient prime checks and shows you can optimize simple loops in real problems.
"What if we changed the loop to check divisors up to n/2 instead of √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 that a prime number has exactly two distinct positive divisors: 1 and itself.Final Answer:
It has exactly two distinct positive divisors: 1 and itself. -> Option AQuick Check:
Prime number = two divisors [OK]
- Thinking all even numbers are prime
- Confusing prime with odd numbers
- Assuming 1 is prime
n is prime?Solution
Step 1: Analyze loop range for prime check
We check divisors from 2 up to but not including n to find any divisor.Step 2: Check correctness of each option
for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; correctly checks divisors from 2 to n-1 and returns false if divisible, else true. for (int i = 2; i <= n; i++) { if (n % i == 0) return false; } return true; incorrectly includes n itself, causing false negatives. for (int i = 1; i < n; i++) { if (n % i == 0) return false; } return true; starts from 1, which divides all numbers. for (int i = 2; i < n/2; i++) { if (n % i == 0) return true; } return false; returns true on first divisor found, which is incorrect logic.Final Answer:
for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; -> Option AQuick Check:
Loop from 2 to n-1 and check divisibility [OK]
- Checking divisibility by 1 or n itself
- Looping beyond n
- Returning true too early
int n = 11;
bool isPrime = true;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
std::cout << (isPrime ? "Prime" : "Not Prime") << std::endl;Solution
Step 1: Understand the loop and condition
The loop checks divisors from 2 to n/2 (which is 5 for n=11). It tests if 11 is divisible by any number in this range.Step 2: Check divisibility for each i
11 % 2 != 0, 11 % 3 != 0, 11 % 4 != 0, 11 % 5 != 0. No divisor found, so isPrime remains true.Final Answer:
Prime -> Option DQuick Check:
11 has no divisors except 1 and itself [OK]
- Looping up to n instead of n/2
- Not breaking loop on divisor found
- Misinterpreting output
n is prime:bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) return true;
}
return false;
}Solution
Step 1: Analyze return values on divisor found
The code returns true immediately when a divisor is found, which means it claims the number is prime incorrectly.Step 2: Correct logic for prime check
When a divisor is found, the function should return false (not prime). If no divisor is found, return true.Final Answer:
Returns true when a divisor is found, should return false instead. -> Option BQuick Check:
Return false on divisor found [OK]
- Returning true on divisor found
- Loop starting at 1
- Not handling n <= 1 correctly
n. Which code snippet correctly implements this?bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}Solution
Step 1: Understand optimization using square root
Any divisor larger than sqrt(n) pairs with a smaller divisor, so checking up to sqrt(n) is enough.Step 2: Verify code logic
The loop runs from 2 to sqrt(n), returns false if divisor found, else true after loop ends. This is correct and efficient.Final Answer:
Correct: checks divisors up to sqrt(n) and returns false if divisible. -> Option CQuick Check:
Check divisors up to sqrt(n) for efficiency [OK]
- Looping up to n/2 instead of sqrt(n)
- Starting loop at 1
- Returning true inside loop on divisor found
Ready to prove it?
Pass the test and this pattern moves unknown
