sieve of eratosthenes

Solutions on MaxInterview for sieve of eratosthenes by the best coders in the world

showing results for - "sieve of eratosthenes"
Giada
03 Apr 2016
1//sieve of eratosthenes or prime of sieve
2#include<iostream>
3#include<math.h>
4using namespace std;
5void primeofsieve(long long int n)
6{
7	long long int arr[n]={};
8	for(int i=2;i<=sqrt(n);i++)
9	{
10		for(long long int j=i*i;j<=n;j+=i)
11			arr[j]=1;
12	}
13	for(long long int i=2;i<=n;i++)
14	{
15	    if(arr[i]==0)
16	    	cout<<i<<" ";
17	}
18
19
20}
21int main()
22{
23
24	#ifdef _DEBUG
25	freopen("input.txt", "r", stdin);
26	freopen("output.txt", "w", stdout);
27    #endif
28	long long int n;
29	cin>>n;
30	cout<<"PRIME NUMBERs ARE : ";
31	primeofsieve(n);
32	return 0;
33}
Elisa
16 Aug 2019
1
2find primes up to N
3For all numbers a : from 2 to sqrt(n)
4     IF a is unmarked THEN
5         a is prime
6         For all multiples of a (a < n)
7             mark multiples of as composite
8All unmarked nummbers are prime!
9
Carl
09 Sep 2020
1function solution(n) {
2   const numArr = new Array(n + 1);
3   numArr.fill(true);
4	// from 1 to n, if number is NOT a prime, change true to false in numArr
5   numArr[0] = numArr[1] = false;
6   for (let i = 2; i <= Math.sqrt(n); i++) {
7      for (let j = 2; i * j <= n; j++) {
8          numArr[i * j] = false;
9      }
10   }
11  	// find number of true(number of prime) by filtering true boolean
12   return numArr.filter(Boolean).length;
13}
14
15
Valentina
06 Jan 2017
1function solution(n) {
2   const numArr = new Array(n + 1);
3   numArr.fill(true);
4   numArr[0] = numArr[1] = false;
5   for (let i = 2; i <= Math.sqrt(n); i++) {
6      for (let j = 2; i * j <= n; j++) {
7          numArr[i * j] = false;
8      }
9   }
10   return numArr.filter(Boolean).length;
11}
12
13
Amin
27 Jan 2017
1
2import java.util.Scanner;
3
4public class BooleanPrimes
5{
6
7    public static int counter = 0 ;
8    public static void main(String[] argh)
9    {
10        Scanner scanner = new Scanner(System.in);
11        System.out.println("Enter a number: ");
12        int size = scanner.nextInt();
13        boolean[] boolArray = new boolean[size+1];
14        printArray( generateBoolArray(boolArray,size+1),size);
15    }
16
17    public static boolean[] generateBoolArray(boolean[] boolArr, int size) // initializing boolean array with true values
18    {
19        for (int i = 2; i < size; ++i)
20        {
21            boolArr[i] = true;
22        }
23        return chickIfIndexPrime(boolArr, boolArr.length,size);
24    }
25
26    public static boolean[] chickIfIndexPrime(boolean[] arrIsPrime, int input, int size)
27    {
28        int start = 2;
29        while (start*start <= input) // first+second loop checking if start is prime
30        {
31            int i = 2;
32            boolean isprime = true;
33            while(i*i < start && isprime ) // second loop
34            {
35                if(start%i == 0)
36                {
37                    isprime = false;
38                }
39                ++i;
40            }
41            if(isprime==true)
42            {
43                for(int j=4; j<arrIsPrime.length;++j) // third loop checking if the index of the array is prime
44                {
45
46                    if(j%start ==0 )
47                    {
48                        if(j == start)
49                        { 
50                            ++j;
51                            if(j >=arrIsPrime.length)
52                            {
53                                break;
54                            }
55                        }
56                        arrIsPrime[j]=false;
57                    }
58                }
59            }
60            ++start;
61
62        }
63        return arrIsPrime;
64    }
65
66
67
68    public static void printArray(boolean[] arr ,int size){
69        System.out.println("The prime numbers from 2 till "+(size));
70        int i=0,j = 0 ;
71        while(i<arr.length){
72            if ( arr[i] == true ) {
73                System.out.print(i+" ");
74                ++counter;
75
76            }
77            ++i;
78        }
79
80        System.out.println();
81        System.out.println("\nIn total there is "+counter+" prime numbers");
82
83    }
84
85}
86
87
88
89
90
91
92
93
94
95
96
queries leading to this page
logic for sieve of eratosthenessieve of eratosthenes practicesievegeekssieve of erostheseswhat is the sieve of eratosthenescheck prime sieve of eratostheneshow to find prime numbers seize of estrogensieve of eratosthenes c 2b 2beratosthenes 27 sievesieve geeks for geekssieve of eratosthenes variationssieve of eratostessieve prime numberefficient algorithm to find prime given an arraysieve of eratosthenes psuedocodeoptimize prime sieve in c 2b 2bsieves algorithm sieve of eratosthenesin sieve prime why use i 2aigenerate a list of all numbers between 0 and n 3b mark the numbers 0 and 1 to be not primesieve of eratosthenes algorith 2ccalculate prime numbers with sieve of eratosthenespseudo code for sieve of eratosthenessieve of erathowhat is the use of sieve of eratosthenes 3f write down the pseudocode for it complexity of finding all prime numbersprime of sive questioncheck if a number is prime using sievesieve of eratosthenes other namesieve of eratosthenes c 2b 2bsieve of eratosthenes log c 2b 2ba program that uses the sieve of eratosthenes to find all the prime numbers between 2 and 5000 prime seivealll prime numbers using seive of eratostethens sieve of eratosthenes when seive code is used in prime numbershwo to generate sieve of prime numbers for largesieve of eratosthenes pseudocodeeratosthenes prime sievesieve c 2b 2bsieve of eratosthenes insieve code in c 2b 2boptimization sevent of erastotenessieve of eratosthenesieve of eratosthenes counting prime numbers arraysieve of eratosthenes why do i 2aisieve of eratosthenes code for finding prime numbers upto a n inc 2b 2bseive codesieve primeswhat is the sieve primessieve prime numbersprime numbers using sieve of eratostheneseratosthenes sieve codesieve of eratosthenes method with an exampleprimes sieve ofsieve of eratosthenes program in c using functionssieve of eratosthenes c 2b 2b prooferatosthenes algorithmsieve of eratosthenes program in coptimization time in sieve of eratosthenes code csieve of eratosthenes proime numbersieve of eratostehenesoptimization time in sieve of eratosthenes in c sieve implimentationsieve gfgprime sieve methodsieve of eratosthenes codeyouwhat is sieve of eratosthenespractical usages of sieve of eratosthenesdoes the sieve of eratosthenes worksieve of eratosthenes 5csieve of 5cprime numbers less than k c 2b 2b nlogn sievesieve of erathsosieve of primesieve of eratosthenes code in c 2b 2bsieve of erathosessieveoferatosthenesmost efficient way to generate prime numbers till a given n valueeratosthen sieve csieve of eratosthenes examples 22sieve of eratosthenes c program time execution optimizationsieve of eratosthenes generator first n prme numberssieve of eratosthenes in cppprimes sieveowing the original and n c3 a4ive definition of the method 3a starting from 2 2c mark all the multiples of that number as not primes and then move to the next unmarked number when there are no more numbers left 2c the unmarked numbers are the primes example 3aoptimization seven of erastothenes cerasthonus sievesieve of eratosthmesbuilt in function for sieve of eratosthenesprinting prime numbers seivewhich of the following algorithm 28s 29 uses prime numbersthe sieve of eratosthenesprime number sieve c 2b 2bsieve algorithmc 2b 2b sieve of eratosthenesprime number java sieve of eratosthenessieve prime algorithmprime numbers between c 2b 2b using sievesieve of eratosthenes i 5e2 reasonwhat is sieve of eratostheneswhy does the sieve of eratosthenes is correcta program that uses the sieve of eratosthenes to find all the prime numbers between 2 and 5000 c 2b 2bis prime sievesieve of eratosthenes cprime sieve of size 1000000sieve of eratosthenes simple prime algorithm implementionc program for sieve of eratosthenes simple sieve of eratosthenessieve of eratosthenes algorithm sieve of eratosthenes c program feratosthenes sieve c 2b 2beratostheens sieveprime sieve c 2b 2bisieve of primes c 2b 2bsieve of eratosthenes n number of prime numbersprint first n prime numbers using sievesieve of eratosthenes 0 28n 29primes number sievesimple prime sieveeratosthenes e2 80 99s sieve algorithmsieve of eratosthenes cppoptimization time seven of erastothenes csieve eratostenusing sieve of eratosthenesprime sieve algorithmsapplication of sieve of eratosthenessieve of ertostheneseratosthenes sieve cranged sieve of eratosthenes javasieve of eratosthenes pronunciationsieve algorithm in c sieve prime complexitysieve eratostheneseratosthenes prime numbers sieveseive geekssieve of eratosthenes in cfind all prime numbers less than 100 gfg code using sieve of erasthseives algo for primessieve primesieve of eratosthenes explainedsieve of eratosthenes implementationa question based on gcd with the sieve of eratosthenes most efficient prime number algorithmseive odf erasthonesesieve de eratostenesgenerating n prime numbers using seive of err in c 2b 2bsieve algorithm 3fprime numbers sieve of eratostheneswhat is sieve of eratosthenes used for 3fprime seive codesieve of eranthosessieve of eratosthenes for prime numbers 23include 3cbits 2fstdc 2b 2b h 3e using namespace std 3b 23define max 10000000 bool prime 5bmax 2b1 5d 3b void sieveoferatosthenes 28 29sieve of eratosthenes exampleeratosth c3 a8ne sievesieve of eratosthenes implementation at gfgcode for sieve of eratostheneswhy does the sieve of eratosthenes worksieve method of number of primesdevelop an algorithm which finds the prime number up to ngfg sieve of eratostheneserasthones sievesieve of eratosthenes c programsieve for primeseratosthenes 26rsquo 3bs sieve algorithmsieve of prime numberssieve algorithm javasieve of erathoneseseratosthenes 27 sieve cprime eratosthenes in cppsieve of eratosthenes prime numberssieve of eratosthenes algorithmprime number sieve of eratosthenessieve of eresthossis gfgsieve and prime sievesieve of eratosthenes usefind the prime no in range cpp seivean algorithm which finds the prime number up to n 28find all the prime numbers from 1 to n using sieve of erastothenessieve of erasieve of primessieve of eratosthenessieve of eratossieve of eratosthenes approachsieve eratosthenesieve of eratosthenes javaprime1 spoj sieve of eratosthenesfind prime numbers algorithmsieve of eratosthenes size 10c program for sieve of eratosthenes functionsprime of sievehow old is sieve of eratosthenesprint prime numbers up to less than n in c in o 28n 29 time complexityfirst n prime numbers using sieve of eratosthenesprime sieve function c 2b 2bsieve of eratosthenes algorithm in cpseudocode of sieve of eratosthenessieve eratosthenes prime numbersalgorithm for finding all prime numbers up to any given big limitsieve algorithm prime numbersprime number sievefind first n prime numbers c 2b 2b sieve of eratosthenesprime sievesieve algorithm for finding prime numbersprint first n prime numbers in c 2b 2b seive of eratosthenesthe sieve of eratosthenes is an algorithm for finding all prime numbers less than or equal to a number n read about this algorithm on wikipedia and implement it in a python program filename 3a find primes pyhw sieve of eratosthenes time funciton in cseive of erathones codeeratosthenes sievehw sieve of eratosthene optimization medium time function in csieve of erastosthenessieve algosieve of eratosthenesseive algorithmsieve of eratosthenes