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 :: c# method returns multiple values 
Csharp :: unity soft body 
Csharp :: unity c# move transform 
Csharp :: bytes size c# 
Csharp :: dynamically add rows to datagridview c# 
Csharp :: audiosource unity 
Csharp :: print pdf in c# 
Csharp :: max index array c# 
Csharp :: C# linq mselect 
Csharp :: how to use buildcontext in initstate flutter 
Csharp :: ultimate space cruiser 
Csharp :: entity framework with query C# 
Csharp :: c# generate random int list 
Csharp :: foreach c# linq example 
Csharp :: Remove access to admin from deleting the file in C# 
Csharp :: string trin c# 
Csharp :: c# read excel file into datatable 
Csharp :: unity rotate around point 
Csharp :: xamarin set environment variables 
Csharp :: how to find length of list c# 
Csharp :: how to get relative path in c# 
Csharp :: pick random point inside box collider unity 
Csharp :: unity cannot click button 
Csharp :: toLocalIsoString() vs toIsoString() 
Csharp :: You can get events when an object is visible within a certain camera, and when it enters or leaves, using these functions: 
Csharp :: unity awake 
Csharp :: Non-Serialized Fields in unity inspector 
Csharp :: c# list contains null 
Csharp :: array sum c# 
Csharp :: how to check to see if the keyboard buttons are pressed in unity 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =