Challenge - 5 Problems
Prime Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Prime Check for 29
What is the output of this Go code that checks if 29 is prime?
DSA Go
package main import "fmt" 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 } func main() { fmt.Println(isPrime(29)) }
Attempts:
2 left
💡 Hint
Remember prime numbers are only divisible by 1 and themselves.
✗ Incorrect
29 is a prime number because it has no divisors other than 1 and 29.
❓ Predict Output
intermediateOutput of Prime Check for 1
What does this Go code print when checking if 1 is prime?
DSA Go
package main import "fmt" 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 } func main() { fmt.Println(isPrime(1)) }
Attempts:
2 left
💡 Hint
Prime numbers are greater than 1.
✗ Incorrect
1 is not prime because prime numbers start from 2.
🔧 Debug
advancedFind the Bug in Prime Check Code
This Go code is supposed to check if a number is prime but gives wrong output for 4. What is the bug?
DSA Go
package main import "fmt" func isPrime(n int) bool { if n <= 1 { return false } for i := 2; i < n; i++ { if n%i == 0 { return false } } return true } func main() { fmt.Println(isPrime(4)) }
Attempts:
2 left
💡 Hint
Check the loop condition for efficiency and correctness.
✗ Incorrect
Looping until i*i <= n is enough to check divisors and improves efficiency.
🧠 Conceptual
advancedWhy Check Divisors Only up to Square Root?
Why does the prime check loop only run until i*i <= n instead of i < n?
Attempts:
2 left
💡 Hint
Think about factor pairs of a number.
✗ Incorrect
If n = a * b and both a and b > sqrt(n), then a*b > n, which is impossible. So one factor must be <= sqrt(n).
❓ Predict Output
expertOutput of Prime Check for Large Number 104729
What is the output of this Go code checking if 104729 is prime?
DSA Go
package main import "fmt" 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 } func main() { fmt.Println(isPrime(104729)) }
Attempts:
2 left
💡 Hint
104729 is a known prime number.
✗ Incorrect
104729 is the 10000th prime number, so the function returns true.
