Answers for "Sieve of Eratosthene"

6

prime of sieve

//sieve of eratosthenes or prime of sieve
#include<iostream>
#include<math.h>
using namespace std;
void primeofsieve(long long int n)
{
	long long int arr[n]={};
	for(int i=2;i<=sqrt(n);i++)
	{
		for(long long int j=i*i;j<=n;j+=i)
			arr[j]=1;
	}
	for(long long int i=2;i<=n;i++)
	{
	    if(arr[i]==0)
	    	cout<<i<<" ";
	}


}
int main()
{

	#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    #endif
	long long int n;
	cin>>n;
	cout<<"PRIME NUMBERs ARE : ";
	primeofsieve(n);
	return 0;
}
Posted by: Guest on May-27-2020
0

sieve of eratosthenes

function solution(n) {
   const numArr = new Array(n + 1);
   numArr.fill(true);
	// from 1 to n, if number is NOT a prime, change true to false in numArr
   numArr[0] = numArr[1] = false;
   for (let i = 2; i <= Math.sqrt(n); i++) {
      for (let j = 2; i * j <= n; j++) {
          numArr[i * j] = false;
      }
   }
  	// find number of true(number of prime) by filtering true boolean
   return numArr.filter(Boolean).length;
}
Posted by: Guest on July-11-2021

Browse Popular Code Answers by Language