Sieve Algo

PHOTO EMBED

Tue Oct 12 2021 02:14:32 GMT+0000 (Coordinated Universal Time)

Saved by @code_ea

#include <iostream>
#include <limits.h>
using namespace std;

 void sieve(int n)
{
	if(n <= 1)
		return;

	bool isPrime[n+1];

	fill(isPrime, isPrime + n + 1, true);

	for(int i=2; i*i <= n; i++)
	{
		if(isPrime[i])
		{
			for(int j = 2*i; j <= n; j = j+i)
			{
				isPrime[j] = false;
			}
		}
	}

	for(int i = 2; i<=n; i++)
	{
		if(isPrime[i])
			cout<<i<<" ";
	}
}
int main() {
    
    	int n = 18;

		sieve(n);
}
content_copyCOPY

https://ide.geeksforgeeks.org/G6EaBjA3VD