Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Sieve of Eratosthenes

	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;
    }
Comment

Sieve Of Eratosthenes

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;
}
Comment

sieve of eratosthenes

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;
  }
}
Comment

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;
}

Comment

Sieve of Eratosthenes

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;
    }
}
Comment

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("
In total there is "+counter+" prime numbers");

    }

}










Comment

PREVIOUS NEXT
Code Example
Csharp :: unity singleton 
Csharp :: c# obsolete class 
Csharp :: ??= mean C# 
Csharp :: enum in c# 
Csharp :: HTTP Error 500.35 - ASP.NET Core does not support multiple apps in the same app pool 
Csharp :: c# substring reverse 
Csharp :: c# creating an array 
Csharp :: calculator in c# 
Csharp :: remove scenedelegate 
Csharp :: async await c# 
Csharp :: convert string to decimal c# 
Csharp :: c# generic return type in interface 
Csharp :: c# get smallest of 3 numbers 
Csharp :: reverse a linked list C# 
Csharp :: loading player preferences unity 
Csharp :: Why Duplicate "..TargetFrameworkAttribute" c# assemblies created 
Csharp :: unity dropdown 
Csharp :: linq from list c# 
Csharp :: how to customize xunit input 
Csharp :: Unity Object rotation along any axis 
Csharp :: linq select max value from list 
Csharp :: GetComponent<Button().onClick 
Csharp :: print hello world in unity 
Csharp :: c# short to int 
Csharp :: c# signalr console app server example 
Csharp :: c# guid from string 
Csharp :: do while loop in c# 
Csharp :: c# in equivalent 
Csharp :: c# slice array 
Csharp :: c sharp async 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =