📖 Scenario: You are helping a friend who wants to check if a number is prime. A prime number is a number greater than 1 that has no divisors other than 1 and itself.Imagine you have a list of numbers and you want to find out which ones are prime.
🎯 Goal: You will write a Go program that checks if a given number is prime by testing divisibility from 2 up to the number minus one.This will help your friend quickly know if a number is prime or not.
📋 What You'll Learn
Create a variable called number with the value 29
Create a variable called isPrime and set it to true
Use a for loop with variable i from 2 to number - 1
Inside the loop, check if number is divisible by i
If divisible, set isPrime to false and break the loop
After the loop, print "Prime" if isPrime is true, else print "Not Prime"
💡 Why This Matters
🌍 Real World
Checking if a number is prime is useful in cryptography, coding challenges, and mathematical computations.
💼 Career
Understanding loops, conditionals, and boolean logic is essential for software development and algorithm design.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 29.
DSA Go
Hint
Use number := 29 to create the variable.
2
Create the isPrime variable
Create a variable called isPrime and set it to true after the number variable.
DSA Go
Hint
Use isPrime := true to create the variable.
3
Check divisibility with a for loop
Use a for loop with variable i starting from 2 up to number - 1. Inside the loop, check if number % i == 0. If yes, set isPrime to false and use break to exit the loop.
DSA Go
Hint
Use a for loop and inside it an if statement to check divisibility.
4
Print if the number is prime or not
After the loop, use if isPrime to print "Prime". Otherwise, print "Not Prime".
DSA Go
Hint
Use fmt.Println("Prime") or fmt.Println("Not Prime") based on isPrime.
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).