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 TypeScript code that checks if 29 is prime?
DSA Typescript
function isPrime(num: number): boolean {
if (num <= 1) return false;
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(29));Attempts:
2 left
💡 Hint
29 is a prime number because it has no divisors other than 1 and itself.
✗ Incorrect
The function checks divisors up to the square root of 29. Since none divide 29 evenly, it returns true.
❓ Predict Output
intermediateOutput of Prime Check for 1
What is the output of this TypeScript code that checks if 1 is prime?
DSA Typescript
function isPrime(num: number): boolean {
if (num <= 1) return false;
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(1));Attempts:
2 left
💡 Hint
1 is not considered a prime number.
✗ Incorrect
The function returns false immediately for numbers less than or equal to 1.
❓ Predict Output
advancedOutput of Prime Check for 100
What is the output of this TypeScript code that checks if 100 is prime?
DSA Typescript
function isPrime(num: number): boolean {
if (num <= 1) return false;
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(100));Attempts:
2 left
💡 Hint
100 is divisible by 2 and 5.
✗ Incorrect
The function finds that 100 is divisible by 2 and returns false.
❓ Predict Output
advancedOutput of Prime Check for 2
What is the output of this TypeScript code that checks if 2 is prime?
DSA Typescript
function isPrime(num: number): boolean {
if (num <= 1) return false;
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(2));Attempts:
2 left
💡 Hint
2 is the smallest prime number.
✗ Incorrect
The loop condition fails immediately because 2*2 > 2, so the function returns true.
🧠 Conceptual
expertWhy does the prime check loop run only up to the square root?
Why does the prime checking function only test divisors up to the square root of the number?
Attempts:
2 left
💡 Hint
Think about pairs of factors and how they multiply to the number.
✗ Incorrect
If a number has a factor larger than its square root, it must pair with a smaller factor already checked, so checking beyond the square root is unnecessary.
