Answers for "what is sieve of eratosthenes"

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
0

sieve of eratosthenes

function solution(n) {
   const numArr = new Array(n + 1);
   numArr.fill(true);
   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;
      }
   }
   return numArr.filter(Boolean).length;
}
Posted by: Guest on July-11-2021
0

sieve of eratosthenes

import java.util.Scanner;

public class BooleanPrimes
{

    public static int counter = 0 ;
    public static void main(String[] argh)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int size = scanner.nextInt();
        boolean[] boolArray = new boolean[size+1];
        printArray( generateBoolArray(boolArray,size+1),size);
    }

    public static boolean[] generateBoolArray(boolean[] boolArr, int size) // initializing boolean array with true values
    {
        for (int i = 2; i < size; ++i)
        {
            boolArr[i] = true;
        }
        return chickIfIndexPrime(boolArr, boolArr.length,size);
    }

    public static boolean[] chickIfIndexPrime(boolean[] arrIsPrime, int input, int size)
    {
        int start = 2;
        while (start*start <= input) // first+second loop checking if start is prime
        {
            int i = 2;
            boolean isprime = true;
            while(i*i < start && isprime ) // second loop
            {
                if(start%i == 0)
                {
                    isprime = false;
                }
                ++i;
            }
            if(isprime==true)
            {
                for(int j=4; j<arrIsPrime.length;++j) // third loop checking if the index of the array is prime
                {

                    if(j%start ==0 )
                    {
                        if(j == start)
                        { 
                            ++j;
                            if(j >=arrIsPrime.length)
                            {
                                break;
                            }
                        }
                        arrIsPrime[j]=false;
                    }
                }
            }
            ++start;

        }
        return arrIsPrime;
    }



    public static void printArray(boolean[] arr ,int size){
        System.out.println("The prime numbers from 2 till "+(size));
        int i=0,j = 0 ;
        while(i<arr.length){
            if ( arr[i] == true ) {
                System.out.print(i+" ");
                ++counter;

            }
            ++i;
        }

        System.out.println();
        System.out.println("\nIn total there is "+counter+" prime numbers");

    }

}
Posted by: Guest on August-06-2021

Code answers related to "what is sieve of eratosthenes"

Browse Popular Code Answers by Language