Challenge - 5 Problems
Sieve Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sieve for n=10
What is the output of the following TypeScript code that uses the Sieve of Eratosthenes to find all primes up to 10?
DSA Typescript
function sieve(n: number): number[] {
const primes = new Array(n + 1).fill(true);
primes[0] = false;
primes[1] = false;
for (let i = 2; i * i <= n; i++) {
if (primes[i]) {
for (let j = i * i; j <= n; j += i) {
primes[j] = false;
}
}
}
const result: number[] = [];
for (let i = 2; i <= n; i++) {
if (primes[i]) result.push(i);
}
return result;
}
console.log(sieve(10));Attempts:
2 left
💡 Hint
Remember that primes are numbers greater than 1 with no divisors other than 1 and themselves.
✗ Incorrect
The sieve marks multiples of each prime as false. Numbers 4, 6, 8, 9, and 10 are not prime. Only 2, 3, 5, and 7 remain true.
❓ Predict Output
intermediateNumber of primes up to 20
How many prime numbers does the following code find up to 20?
DSA Typescript
function sieveCount(n: number): number {
const primes = new Array(n + 1).fill(true);
primes[0] = false;
primes[1] = false;
for (let i = 2; i * i <= n; i++) {
if (primes[i]) {
for (let j = i * i; j <= n; j += i) {
primes[j] = false;
}
}
}
let count = 0;
for (let i = 2; i <= n; i++) {
if (primes[i]) count++;
}
return count;
}
console.log(sieveCount(20));Attempts:
2 left
💡 Hint
Count primes: 2,3,5,7,11,13,17,19
✗ Incorrect
The primes up to 20 are 2, 3, 5, 7, 11, 13, 17, and 19, totaling 8 primes.
🧠 Conceptual
advancedWhy does the inner loop start at i*i?
In the Sieve of Eratosthenes, why does the inner loop start marking multiples from i*i instead of 2*i?
Attempts:
2 left
💡 Hint
Think about when multiples get marked in previous steps.
✗ Incorrect
Multiples less than i*i are already marked by smaller primes, so starting at i*i avoids redundant work.
❓ Predict Output
advancedOutput of sieve with n=1
What is the output of the following code when n=1?
DSA Typescript
function sieve(n: number): number[] {
const primes = new Array(n + 1).fill(true);
primes[0] = false;
primes[1] = false;
for (let i = 2; i * i <= n; i++) {
if (primes[i]) {
for (let j = i * i; j <= n; j += i) {
primes[j] = false;
}
}
}
const result: number[] = [];
for (let i = 2; i <= n; i++) {
if (primes[i]) result.push(i);
}
return result;
}
console.log(sieve(1));Attempts:
2 left
💡 Hint
Primes start from 2.
✗ Incorrect
Since n=1, no numbers 2 or greater exist, so the result is an empty list.
🔧 Debug
expertIdentify the error in this sieve implementation
What error does the following code produce when run with n=10?
DSA Typescript
function sieve(n: number): number[] {
const primes = new Array(n + 1).fill(true);
primes[0] = false;
primes[1] = false;
for (let i = 2; i <= n / 2; i++) {
if (primes[i]) {
for (let j = i * i; j <= n; j += i) {
primes[j] = false;
}
}
}
const result: number[] = [];
for (let i = 2; i <= n; i++) {
if (primes[i]) result.push(i);
}
return result;
}
console.log(sieve(10));Attempts:
2 left
💡 Hint
Check the loop condition for i in the outer loop.
✗ Incorrect
The code runs correctly for n=10 and outputs [2, 3, 5, 7]. The outer loop condition i <= n/2 works but is suboptimal; the standard Sieve uses i * i <= n to limit the outer loop to sqrt(n) for better efficiency.
