Check if Number is Prime in DSA Go - Time & Space Complexity
We want to understand 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.
func isPrime(n int) bool {
if n <= 1 {
return false
}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return true
}
This code checks if a number n is prime by testing divisors from 2 up to the square root of n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that tests divisors i from 2 up to √n.
- How many times: Approximately √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 checks grows slower than n, roughly like 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 need to check all numbers from 2 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 would have a matching smaller factor already checked.
Understanding this time complexity helps you explain efficient prime checking, a common coding challenge that shows your grasp of loops and optimization.
"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
It has exactly two distinct positive divisors: 1 and itself. matches this definition exactly, while others describe incorrect properties.Final Answer:
It has exactly two distinct positive divisors: 1 and itself. -> Option DQuick Check:
Prime number = two divisors only [OK]
- Thinking even numbers are always prime
- Confusing prime with odd numbers
- Believing 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 if n is divisible by any number other than 1 and itself.Step 2: Evaluate each option's correctness
for i := 2; i < n; i++ { if n%i == 0 { return false } } return true correctly loops from 2 to n-1 and returns false if divisible. for i := 2; i <= n; i++ { if n%i == 0 { return false } } return true incorrectly includes n itself, always returning false. for i := 1; i < n; i++ { if n%i == 0 { return false } } return true starts from 1, which divides all numbers. for i := 2; i < n/2; i++ { if n%i == 0 { return true } } return false returns true on divisor found, which is wrong logic.Final Answer:
for i := 2; i < n; i++ { if n%i == 0 { return false } } return true -> Option AQuick Check:
Loop 2 to n-1 and check divisibility [OK]
- Checking divisibility including n itself
- Starting loop from 1 instead of 2
- Returning true when divisor found
isPrime(11)?func isPrime(n int) bool {
if n <= 1 {
return false
}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return true
}Solution
Step 1: Check input and loop condition
Input n=11 is greater than 1, so function proceeds. Loop runs from i=2 while i*i <= 11 (i <= 3).Step 2: Test divisibility inside loop
Check i=2: 11%2 != 0; i=3: 11%3 != 0. No divisor found, loop ends.Final Answer:
true -> Option AQuick Check:
11 is prime, function returns true [OK]
- Not handling n <= 1 correctly
- Looping beyond sqrt(n)
- Assuming 11 is not prime
func isPrime(n int) bool {
if n <= 1 {
return false
}
for i := 2; i < n/2; i++ {
if n%i == 0 {
return false
}
}
return true
}Solution
Step 1: Analyze loop range and condition
The loop runs from 2 to less than n/2, which is inefficient and may miss divisors for small n.Step 2: Suggest correct loop condition
Looping until i*i <= n is both correct and efficient because any divisor larger than sqrt(n) pairs with a smaller divisor already checked.Final Answer:
Loop should run until i*i <= n for efficiency and correctness -> Option CQuick Check:
Use i*i <= n to check divisors [OK]
- Looping only to n/2 misses some divisors
- Returning true for n <= 1
- Misusing modulo operator
Solution
Step 1: Understand prime checking for multiple numbers
Checking each number from 2 to 20 individually is necessary to find all primes.Step 2: Optimize divisibility checks
Testing divisibility only up to sqrt(n) for each number reduces unnecessary checks and improves performance.Final Answer:
Check each number from 2 to 20 using a function that tests divisibility up to sqrt(n) -> Option BQuick Check:
Use sqrt(n) limit for each number check [OK]
- Checking all numbers up to n-1 wastes time
- Ignoring odd numbers which can be prime
- Marking all numbers prime without checks
Ready to prove it?
Pass the test and this pattern moves unknown
