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 C# code that uses the Sieve of Eratosthenes to find all primes up to 10?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int n = 10; bool[] isPrime = new bool[n + 1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } } List<int> primes = new List<int>(); for (int i = 2; i <= n; i++) { if (isPrime[i]) primes.Add(i); } Console.WriteLine(string.Join(", ", primes)); } }
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. For n=10, primes are 2, 3, 5, and 7 only.
❓ Predict Output
intermediateNumber of primes up to 20
How many prime numbers does the following code find up to 20?
DSA C#
int n = 20; bool[] prime = new bool[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } int count = 0; for (int i = 2; i <= n; i++) { if (prime[i]) count++; } Console.WriteLine(count);
Attempts:
2 left
💡 Hint
List primes up to 20: 2,3,5,7,11,13,17,19
✗ Incorrect
There are exactly 8 primes up to 20: 2,3,5,7,11,13,17,19.
🔧 Debug
advancedIdentify the error in Sieve implementation
What error does the following code produce when run?
DSA C#
int n = 15; bool[] prime = new bool[n]; for (int i = 2; i <= n; i++) prime[i] = true; for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } int count = 0; for (int i = 2; i <= n; i++) { if (prime[i]) count++; } Console.WriteLine(count);
Attempts:
2 left
💡 Hint
Check array size and indexing carefully.
✗ Incorrect
The array size is n (15), but code accesses prime[15], which is out of bounds since valid indices are 0 to 14.
🧠 Conceptual
advancedWhy start marking multiples from i*i?
In the Sieve of Eratosthenes, why do we start marking multiples of a prime number i from i*i instead of 2*i?
Attempts:
2 left
💡 Hint
Think about when smaller multiples get marked.
✗ Incorrect
Multiples less than i*i are already marked by smaller primes, so starting at i*i avoids redundant work.
❓ Predict Output
expertOutput of Sieve with n=30 and printing primes in ascending order
What is the output of this C# code that finds primes up to 30 using the Sieve of Eratosthenes and prints them in ascending order?
DSA C#
int n = 30; bool[] prime = new bool[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } for (int i = 2; i <= n; i++) { if (prime[i]) Console.Write(i + " "); }
Attempts:
2 left
💡 Hint
Remember that primes do not include composite numbers like 9, 25, or 27.
✗ Incorrect
The Sieve correctly marks all non-primes false. The primes up to 30 are 2,3,5,7,11,13,17,19,23,29.
