“peneira de eratóstenes” Respostas de código

Peneira de eratóstenes

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

peneira de eratóstenes

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

peneira de eratóstenes

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

Cautious Cockroach

Peneira de eratóstenes

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

peneira de eratóstenes


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");

    }

}










Mero

Procure respostas de código populares por idioma

Procurar outros idiomas de código