Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions C++/sieve/yathansh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// This functions finds all primes smaller than 'limit'
// using simple sieve of eratosthenes.
void simpleSieve(int limit)
{
// Create a boolean array "mark[0..limit-1]" and
// initialize all entries of it as true. A value
// in mark[p] will finally be false if 'p' is Not
// a prime, else true.
bool mark[limit];
memset(mark, true, sizeof(mark));

// One by one traverse all numbers so that their
// multiples can be marked as composite.
for (int p=2; p*p<limit; p++)
{
// If p is not changed, then it is a prime
if (mark[p] == true)
{
// Update all multiples of p
for (int i=p*2; i<limit; i+=p)
mark[i] = false;
}
}

// Print all prime numbers and store them in prime
for (int p=2; p<limit; p++)
if (mark[p] == true)
cout << p << " ";
}