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 n is divisible by a number greater than its square root, it must also be divisible by a number smaller than the square root. So checking up to the square root is enough.
Click to reveal answer
beginner
What is the simplest way 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 divisors only up to the square root of n.
Click to reveal answer
beginner
In Java, which operator is used to check if one number divides another evenly?
The modulus operator '%' is used. For example, 'n % i == 0' means i divides n evenly.
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 29 is prime, up to which number should you check for divisors?
A28
B5
C14
D29
✗ Incorrect
Check divisors up to √29, which is approximately 5.38, so up to 5.
What does the expression 'n % i == 0' mean in Java?
An is divisible by i
Bn is not divisible by i
Cn equals i
Di is greater than n
✗ Incorrect
The modulus operator '%' returns the remainder. If remainder is 0, n is divisible by i.
Which of these numbers is prime?
A15
B9
C1
D11
✗ Incorrect
11 is prime; 1 is not prime, 9 and 15 have divisors other than 1 and themselves.
What is the smallest prime number?
A2
B1
C0
D3
✗ Incorrect
2 is the smallest prime number and the only even prime number.
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.
Write the steps to implement a prime number check in Java.
Focus on the loop and the modulus check.
You got /4 concepts.
Practice
(1/5)
1. What does it mean for a number to be 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 prime numbers have 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 numbers have only 1 and itself as divisors [OK]
Common Mistakes:
Thinking even numbers are always prime
Confusing prime with odd numbers
Assuming 1 is prime
2. Which of the following Java code snippets correctly checks if a number n is prime?
easy
A. for (int i = 2; i < n / 2; i++) { if (n % i == 0) return true; } return false;
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; i++) { if (n % i == 0) return false; } return true;
Solution
Step 1: Analyze the loop range for checking divisors
We check divisors from 2 up to but not including n to see if n is divisible by any number other than 1 and itself.
Step 2: Check each option's logic
for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; correctly loops from 2 to n-1 and returns false if a divisor is found, 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 finding a divisor but should return false.
Final Answer:
for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; -> Option D
Quick Check:
Loop 2 to n-1 and check divisibility [OK]
Hint: Loop from 2 to n-1 to check divisors [OK]
Common Mistakes:
Checking divisibility including n itself
Starting loop from 1 instead of 2
Returning true when divisor found
3. What is the output of the following Java code when n = 11?
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
System.out.println(isPrime);
medium
A. true
B. false
C. Compilation error
D. Runtime exception
Solution
Step 1: Understand the loop range and condition
The loop runs from 2 to the square root of 11 (approximately 3.3), so i takes values 2 and 3.
Step 2: Check divisibility for i=2 and i=3
11 % 2 != 0 and 11 % 3 != 0, so no divisor found, isPrime remains true.
Final Answer:
true -> Option A
Quick Check:
11 is prime, so output is true [OK]
Hint: Check divisors only up to sqrt(n) for efficiency [OK]
Common Mistakes:
Looping up to n instead of sqrt(n)
Forgetting to break loop on divisor found
Misunderstanding boolean flag usage
4. Find the error in the following Java code that checks if n is prime:
boolean isPrime = true;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = true;
break;
}
}
System.out.println(isPrime);
medium
A. The loop should run until i < n, not n / 2.
B. The flag isPrime should be set to false when divisor found, not true.
C. The loop variable i should start from 1, not 2.
D. There is no error; the code works correctly.
Solution
Step 1: Analyze the flag update inside the loop
When a divisor is found, isPrime should be set to false, but here it is incorrectly set to true.
Step 2: Check loop range and starting point
The loop from 2 to n/2 is acceptable for checking divisors, so no error there.
Final Answer:
The flag isPrime should be set to false when divisor found, not true. -> Option B
Quick Check:
Set isPrime = false on divisor found [OK]
Hint: Set prime flag false when divisor found [OK]
Common Mistakes:
Setting isPrime true inside divisor check
Looping from 1 instead of 2
Ignoring break after finding divisor
5. You want to write a Java method isPrime(int n) that returns true if n is prime and false otherwise. Which of the following improvements makes the method more efficient for large n?
hard
A. Check divisibility by all numbers from 2 to n-1 without any break.
B. Check divisibility only by even numbers.
C. Check divisibility only up to the square root of n instead of n-1.
D. Check divisibility by numbers greater than n/2 only.
Solution
Step 1: Understand the divisor checking range
Any divisor larger than the square root of n would have a corresponding divisor smaller than the square root, so checking beyond sqrt(n) is unnecessary.
Step 2: Evaluate options for efficiency
Check divisibility only up to the square root of n instead of n-1. reduces the number of checks drastically, improving efficiency. Check divisibility by all numbers from 2 to n-1 without any break. is inefficient. Check divisibility only by even numbers. is wrong because even numbers except 2 are not prime. Check divisibility by numbers greater than n/2 only. misses smaller divisors.
Final Answer:
Check divisibility only up to the square root of n instead of n-1. -> Option C
Quick Check:
Check divisors up to sqrt(n) for efficiency [OK]
Hint: Only check divisors up to sqrt(n) for speed [OK]
Common Mistakes:
Checking all numbers up to n-1
Checking only even numbers
Ignoring divisor pairs below sqrt(n)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.