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 way to check if a number n is prime?
Check if n is divisible by any number from 2 up to sqrt(n). If none divide n evenly, then n is prime.
Click to reveal answer
beginner
In Go, which loop structure is commonly used to check divisors for primality?
A for loop is used to iterate from 2 to sqrt(n) to check if n is divisible by any number in that range.
Click to reveal answer
beginner
What should the function return if the input number is less than 2?
It should return false because numbers less than 2 are not prime by definition.
Click to reveal answer
What is the smallest prime number?
A1
B3
C2
D0
✗ Incorrect
2 is the smallest prime number because it is greater than 1 and divisible only by 1 and itself.
When checking if 29 is prime, up to which number should you check divisors?
A5
B6
C29
D14
✗ Incorrect
Check divisors up to the square root of 29, which is approximately 5.38, so check up to 5.
If a number n > 3 is divisible by 3, what can you say about n?
An is prime
Bn is not prime
Cn is even
Dn is less than 3
✗ Incorrect
Since n > 3 and is divisible by 3, it has a divisor other than 1 and itself, so n is not prime.
Which of these numbers is NOT prime?
A21
B19
C17
D23
✗ Incorrect
21 is divisible by 3 and 7, so it is not prime.
What should a prime checking function return for input 1?
Aundefined
Btrue
Cerror
Dfalse
✗ Incorrect
1 is not a prime number, so the function should return false.
Explain how to check if a number is prime using a simple algorithm.
Think about dividing the number by smaller numbers to find if it has any divisors.
You got /4 concepts.
Describe why checking divisors only up to the square root of a number is enough to determine primality.
Consider how multiplication pairs work for numbers.
You got /3 concepts.
Practice
(1/5)
1. What does it mean for a number to be prime?
easy
A. It has more than two divisors.
B. It is divisible by 2 only.
C. It is an even number greater than 2.
D. It has exactly two distinct positive divisors: 1 and itself.
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 D
Quick Check:
Prime number = two divisors only [OK]
Hint: Prime means only divisible by 1 and itself [OK]
Common Mistakes:
Thinking even numbers are always prime
Confusing prime with odd numbers
Believing 1 is prime
2. Which of the following Go code snippets correctly checks if a number n is prime?
easy
A. for i := 2; i < n; i++ { if n%i == 0 { return false } } return true
B. for i := 2; i <= n; i++ { if n%i == 0 { return false } } return true
C. for i := 1; i < n; i++ { if n%i == 0 { return false } } return true
D. for 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 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 A
Quick Check:
Loop 2 to n-1 and check divisibility [OK]
Hint: Loop from 2 to n-1, return false if divisible [OK]
Common Mistakes:
Checking divisibility including n itself
Starting loop from 1 instead of 2
Returning true when divisor found
3. What will be the output of this Go function call: 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
}
medium
A. true
B. false
C. runtime error
D. infinite loop
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).