Mastering Prime Numbers: Learn the Sieve Algorithm in C++ for Beginners

RuchikaRawaniRuchikaRawani
3 min read

Table of contents

What are Prime Numbers?

In simpler terms, they are numbers that can only be divided by 1 and the number itself.

Introducing the Sieve Algorithm

The Sieve Algorithm is a brilliant and efficient way to find all prime numbers up to a given limit. How does it work, you ask? Let me break it down into easy steps:

  1. Create a List: First, create a list of numbers from 2 to your desired limit.

  2. Start Crossing Off: Begin with the first number in the list (which is 2) and cross out all of its multiples. Move to the next number that hasn’t been crossed out and do the same.

  3. Repeat: Repeat step 2 until you reach the end of the list. The remaining numbers that haven't been crossed out are your prime numbers!

  4. Here's the Sieve Algorithm implemented in C++ with explanations for beginners:

  5. Explanation:

  6. #include <iostream> and #include <vector>: These are header files for input/output operations and vectors, respectively. They allow us to use functions like cout for output and vector for dynamic arrays.

  7. vector<bool> primes(limit + 1, true);: This line creates a vector of boolean values initialized with true representing numbers from 0 to the limit. Initially, all numbers are considered prime.

  8. for (int num = 2; num * num <= limit; ++num): This loop iterates through numbers from 2 to the square root of the limit. If num is prime (marked as true), it marks its multiples as false.

  9. vector<int> prime numbers; This line creates an empty vector to store prime numbers.

  10. for (int num = 2; num <= limit; ++num) { if (primes[num]) {prime numbers.push_back(num); } }: After marking non-prime numbers, this loop goes through the boolean vector. If a number is marked as true, it means it's prime, so it's added to the prime numbers vector.

  11. cout << "Prime numbers up to " << limit << " are: "; This line prints a message indicating the range of prime numbers being displayed.

  12. for (int num : prime numbers) { cout << num << " "; } cout << endl;: This loop prints the prime numbers stored in the prime numbers vector.

  13. Conclusion

    In conclusion, the Sieve Algorithm is an efficient method for finding prime numbers up to a given limit. By understanding and implementing this algorithm in C++, beginners can enhance their coding skills and explore the fascinating world of prime numbers. With practice and curiosity, you might even contribute to significant mathematical discoveries in the future. Happy coding!


    Feel free to customize the blog as per your style, and don't forget to add any additional information or examples if you feel they would enhance the understanding of the topic for your readers. Good luck with your blog on Hashnode!

6
Subscribe to my newsletter

Read articles from RuchikaRawani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RuchikaRawani
RuchikaRawani

Hey there! I'm Ruchika Rawani, a second-year student at LPU Punjab, pursuing BCA. Currently immersed in the world of Data Structures and Algorithms (DSA), I am also exploring the realms of web development. As a fellow beginner, I channel my learning journey into insightful blogs tailored for beginners. Writing not only serves as a means of sharing knowledge but also acts as a powerful tool for solidifying my own understanding of complex topics. Stay tuned as I continue to unravel the mysteries of coding and web development through my engaging and beginner-friendly blogs.