Simple Sieve
Simple Sieve
URL:https://forms.gle/YDWLEoCL7apATTAU8
QR CODE:
Simple Sieve
SIMPLE SIEVE
find primes up to N
For all numbers a: from 2 to sqrt(n)
IF a is unmarked THEN
a is prime
For all multiples of an (a < n)
mark multiples of as a composite
All unmarked numbers are prime!
EXAMPLE
After applying the Sieve of Eratosthenes, it will produce the list of prime numbers 2, 3, 5, 7
ALGORITHM
Step 4) Repeat the previous step until the value of x should be lesser
than or equal to the square root of n (x<= )
Step 5) After those four steps, the remaining unmarked numbers would be
all the primes on that given range n.
Representation
EXAMPLE
Step 2) Then we select the smallest number on the list, x. Initially x=2
as it is the smallest prime number. Then we traverse through the list and
mark the multiples of 2.
The multiples of 2 for the given value of n is: 4, 6, 8, 10, 12, 14, 16,
18, 20, 22, 24.
Eliminated multiples of 2
Eliminated multiples of 3
Representation EXAMPLE
Eliminated multiples of 5
CIRCULAR LINKED LIST EXAMPLE
import java.util.*;
public class Main{ System.out.println("List of prime
public static void main(String args[]) numbers upto given number are : ");
{ for (int i = 2; i< bool.length; i++) {
Scanner sc = new Scanner(System.in); if(bool[i]==true) {
System.out.println("Enter a number"); System.out.println(i);
int num = sc.nextInt(); }
boolean[] bool = new boolean[num]; }
for (int i = 0; i< bool.length; i++) { }
bool[i] = true; }
}
for (int i = 2; i< Math.sqrt(num);i++){
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}}
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra
https://learn.codemithra.com