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 Function for 29
What is the output of the following C++ code when checking if 29 is prime?
DSA C++
#include <iostream> #include <cmath> bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= std::sqrt(n); ++i) { if (n % i == 0) return false; } return true; } int main() { int num = 29; if (isPrime(num)) std::cout << "29 is prime" << std::endl; else std::cout << "29 is not prime" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check divisors from 2 up to square root of the number.
✗ Incorrect
29 is a prime number because it has no divisors other than 1 and itself. The loop checks divisors up to sqrt(29) which is about 5.3, and finds none.
❓ Predict Output
intermediateOutput for Number 1 in Prime Check
What will the output be when checking if 1 is prime using the same function?
DSA C++
#include <iostream> #include <cmath> bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= std::sqrt(n); ++i) { if (n % i == 0) return false; } return true; } int main() { int num = 1; if (isPrime(num)) std::cout << "1 is prime" << std::endl; else std::cout << "1 is not prime" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember prime numbers are greater than 1.
✗ Incorrect
The function returns false for 1 because it is not greater than 1, so output is '1 is not prime'.
🔧 Debug
advancedIdentify the Error in Prime Check Loop
What error will this code produce when checking if 25 is prime?
DSA C++
#include <iostream> #include <cmath> bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i < std::sqrt(n); ++i) { if (n % i == 0) return false; } return true; } int main() { int num = 25; if (isPrime(num)) std::cout << "25 is prime" << std::endl; else std::cout << "25 is not prime" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop uses i < sqrt(n) instead of i <= sqrt(n), so it misses checking divisor 5 for 25. Thus, it incorrectly returns true.
❓ Predict Output
advancedOutput of Prime Check for Large Number 97
What is the output of this code when checking if 97 is prime?
DSA C++
#include <iostream> #include <cmath> bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= std::sqrt(n); ++i) { if (n % i == 0) return false; } return true; } int main() { int num = 97; if (isPrime(num)) std::cout << "97 is prime" << std::endl; else std::cout << "97 is not prime" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check divisors up to sqrt(97) which is about 9.8.
✗ Incorrect
97 is prime because no divisor from 2 to 9 divides it evenly.
🧠 Conceptual
expertWhy Check Divisors Only up to Square Root?
Why does the prime checking function only check divisors up to the square root of the number?
Attempts:
2 left
💡 Hint
Think about pairs of factors.
✗ Incorrect
If n = a * b and both a and b were greater than sqrt(n), then a * b would be greater than n, which is impossible. So one factor must be less than or equal to sqrt(n).
