A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Click to reveal answer
intermediate
Why do we only check divisors up to the square root of a number when testing for primality?
Because if a number has a divisor larger than its square root, it must also have a smaller divisor below the square root. So checking up to the square root is enough to find any divisor.
Click to reveal answer
beginner
What is the simplest method to check if a number n is prime?
Try dividing n by every number from 2 up to n-1. If none divide evenly, n is prime. This is simple but slow for large n.
Click to reveal answer
intermediate
What is the time complexity of checking primality by testing divisors up to ān?
The time complexity is O(ān) because we check all numbers from 2 up to the square root of n.
Click to reveal answer
beginner
In C++, what is a good way to handle the special cases when checking if a number is prime?
Check if the number is less than 2 (not prime), and handle 2 separately since it is the smallest prime number.
Click to reveal answer
Which number is NOT a prime number?
A2
B3
C4
D5
ā Incorrect
4 is not prime because it can be divided evenly by 2.
When checking if a number n is prime, up to which number should you check for divisors?
An-1
Bn/2
Cn+1
DSquare root of n
ā Incorrect
Checking up to the square root of n is enough to find any divisor.
What is the smallest prime number?
A2
B1
C0
D3
ā Incorrect
2 is the smallest prime number.
If a number is divisible by any number other than 1 and itself, it is:
APrime
BComposite
CEven
DOdd
ā Incorrect
Such a number is composite, not prime.
What is the time complexity of the basic prime check algorithm that tests divisors up to ān?
AO(ān)
BO(log n)
CO(n)
DO(n²)
ā Incorrect
The algorithm runs in O(ān) time.
Explain how to check if a number is prime using the square root optimization.
Think about why checking beyond the square root is unnecessary.
You got /3 concepts.
Describe the edge cases to consider when writing a prime checking function.
Consider very small numbers and the smallest prime.
You got /3 concepts.
Practice
(1/5)
1. What does it mean if a number is prime?
easy
A. It has exactly two distinct positive divisors: 1 and itself.
B. It is divisible by 2 only.
C. It is an even number greater than 2.
D. It has more than two divisors.
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 A
Quick Check:
Prime number = two divisors [OK]
Hint: Prime means divisible only by 1 and itself [OK]
Common Mistakes:
Thinking all even numbers are prime
Confusing prime with odd numbers
Assuming 1 is prime
2. Which of the following C++ code snippets correctly checks if a number n is prime?
easy
A. for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true;
B. for (int i = 2; i <= n; i++) { if (n % i == 0) return false; } return true;
C. for (int i = 1; i < n; i++) { if (n % i == 0) return false; } return true;
D. for (int i = 2; i < n/2; i++) { if (n % i == 0) return true; } return false;
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 A
Quick Check:
Loop from 2 to n-1 and check divisibility [OK]
Hint: Check divisors from 2 to n-1 only [OK]
Common Mistakes:
Checking divisibility by 1 or n itself
Looping beyond n
Returning true too early
3. What is the output of the following C++ code?
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;
medium
A. Runtime Error
B. Not Prime
C. Compilation Error
D. Prime
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.
Hint: Check divisors only up to n/2 for efficiency [OK]
Common Mistakes:
Looping up to n instead of n/2
Not breaking loop on divisor found
Misinterpreting output
4. Find the error in this C++ code that checks if 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;
}
medium
A. Loop should start from 1, not 2.
B. Returns true when a divisor is found, should return false instead.
C. Function should return true for n <= 1.
D. No error, code is correct.
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 B
Quick Check:
Return false on divisor found [OK]
Hint: Return false when divisor found, true otherwise [OK]
Common Mistakes:
Returning true on divisor found
Loop starting at 1
Not handling n <= 1 correctly
5. You want to optimize prime checking for large numbers by checking divisors only up to the square root of 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;
}
hard
A. Incorrect: returns true inside the loop when divisor found.
B. Incorrect: loop should go up to n/2, not sqrt(n).
C. Correct: checks divisors up to sqrt(n) and returns false if divisible.
D. Incorrect: should start loop from 1 instead of 2.
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 C
Quick Check:
Check divisors up to sqrt(n) for efficiency [OK]
Hint: Check divisors only up to sqrt(n) for speed [OK]
Common Mistakes:
Looping up to n/2 instead of sqrt(n)
Starting loop at 1
Returning true inside loop on divisor found
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.