Prime Numbers - Sieve of Eratosthenes
Why Stack Overflow recommends it
Stack Overflow users link it when explaining or implementing the sieve for prime-finding questions, as a quick visual of how the algorithm works.
Recommended by 2 people across 2 different months, 2017 to 2025.
What it is
A short 2012 classroom-style demonstration of the Sieve of Eratosthenes, the ancient method for finding all primes up to a limit. It works through the algorithm on a 100's chart, crossing out multiples step by step. It is published by the Region 10 ESC education channel.
What people said
Recommends DarrylG · 2021-01 · Stack Overflow · on “Why is my program taking so long to execute/is there something wrong with the way I completed the problem?”
@ChillDude4763--The Sieve of Eratosense is one of the most efficient algorithms for finding small prime numbers i.e. up to 10 million or so. There are many description of the algorithm online such as Wikipedia [LINK] and video [this video] . Does this help?
Recommends Khavin S · 2025-09 · Stack Overflow · on “How to check if a number is prime in a more efficient manner?”
Sieve of Eratosthenes algorithm [LINK] I have no idea whether it is more efficient or not, but I watched [this video] and implemented the below code. /*[13] Create a program to find all the prime numbers between 1 and 100. There is a classic method for doing this, called the “Sieve of Eratosthenes.” If you don’t know that method, get on the Web and look it up. Write your program using this method.*/ // This program finds all prime numbers between 1 and 100 using the Sieve of Eratosthenes method. #include "PPP.h"; int main() { vector<int> number_sequence; // generates sequence of numbers from 0 to 100: for (int i = 0; i <= 100; ++i) // starts from 0 to make indexing easier number_sequence.push_bac
Cites oybek · 2017-12 · Stack Overflow · on “Non-Prime Number Counter in C”
Composite numbers is non-prime numbers greater than 1. So all you have to do is to find prime number up to 10k. It can easily be done by erythrophene sieve . The principle of the sieve of Eratosthenes [this video] Create array which maps numbers to {1, 0}, 1 - if number is prime, 0 - if composite. #define SIZE 10001 int is_prime[10000]; Now let's initialize sieve: void init_sieve() { int i, j; for( i = 0; i < SIZE; ++i ) is_prime[i] = 1; is_prime[0] = is_prime[1] = 0; for( i = 2; i < SIZE; ++i ) if( is_prime[i] ) for( j = i*i; j < SIZE; j += i ) is_prime[j] = 0; } So we got bool array, each element of which is 1 if element's index is prime, 0 otherwise. Now, we can simple iterate throw it and get