boolean[] seiveOfEnthorism(int n) {
if(n <= 1)
return new boolean[0];
boolean prime[] = new boolean[n + 1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
int sqr = (int)Math.sqrt(n);
for(int i = 2; i <= sqr; i++) {
if(prime[i]) {
int j = 2;
while(i * j <= n) {
prime[i * j] = false;
j++;
}
}
}
return prime;
}
public static bool[] SieveOfEratosthenes(int num)
{
bool[] isPrime = new bool[num + 1];
for (int i = 2; i <= num; i++) isPrime[i] = true;
// Removing multiples.
for (int i = 2; i <= num; i++)
{
if (isPrime[i])
{
for (int j = i * 2; j <= num; j += i)
isPrime[j] = false; // Eliminate multiples of i.
}
}
return isPrime;
}
internal class Program
{
public static void Main()
{
var n = 53;
var prime = SieveOfEratosthenes(n);
for (int i = 2; i <= n; i++)
{
if(prime[i] == 0)
{
Console.WriteLine(i); ;
}
}
}
public static int[] SieveOfEratosthenes(int num)
{
var isPrime = new int[num + 1];
for (int i = 2; i <= num; i++)
{
if (isPrime[i]==0)
{
for (int j = i * 2; j <= num; j += i)
{
// Eliminate multiples of i.
isPrime[j] = 1;
}
}
}
return isPrime;
}
}
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;
}
int n;
vector<bool> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i] && (long long)i * i <= n) {
for (int j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
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("
In total there is "+counter+" prime numbers");
}
}