Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sieve of Eratosthenes The Sieve of Eratosthenes uses a bag to find all primes less than or equal to an integer value n. Begin by creating a bag an inserting.

Similar presentations


Presentation on theme: "Sieve of Eratosthenes The Sieve of Eratosthenes uses a bag to find all primes less than or equal to an integer value n. Begin by creating a bag an inserting."— Presentation transcript:

1 Sieve of Eratosthenes The Sieve of Eratosthenes uses a bag to find all primes less than or equal to an integer value n. Begin by creating a bag an inserting values from 2 to n. Using values 2, 3, 4, and so forth, remove all multiple, leaving only the prime numbers. E.g. The figure illustrates the sieve of Eratosthenes when finding all prime numbers in the range from 2 through 25.

2 The method sieve(n) public static Bag<Integer> sieve(int n) {
int m, i; Bag<Integer> primeBag = new Bag<Integer>(n); // load the set with integers 2, 3, ..., n for (m = 2; m <= n; m++) primeBag.add(new Integer(m)); // find primes using the Sieve of Eratosthenes; // look at numbers from // m = 2 to m * m > n (m <= sqrt(n)) for (m = 2; m * m <= n; m++)

3 sieve(n) (concluded) // remove all multiples of m starting with 2*m
// check if m is still in the set; if so // remove all multiples of m starting with 2*m if(primeBag.contains(new Integer(m))) { // i sequences through successive multiples // of m, 2*m, 3*m, ... i = 2 * m; while (i <= n) primeBag.remove(new Integer(i)); // update i to the next multiple of m i += m; } return primeBag;

4 Display all primes from 2 to PRIMELIMIT = 500.
Program 8.2 Display all primes from 2 to PRIMELIMIT = 500. import ds.util.Bag; public class Program8_2 { public static void main(String[] args) { final int PRIMELIMIT = 500; Bag<Integer> bag; // call sieve() and return the bag of primes bag = sieve(PRIMELIMIT); // list elements in the bag as an array // output primes in 6 spaces, 10 per line writePrimes(bag.toArray()); System.out.println(); }

5 Program 8.2 (continued) // in 6 spaces, 10 per line
// output elements in the array // in 6 spaces, 10 per line public static void writePrimes(Object[] arr) { String intStr; int count = 1, i; // initialize sb with 6 blanks StringBuffer sb = new StringBuffer(" "); for (i = 0; i < arr.length; i++) { // convert integer to a string intStr = arr[i].toString(); // use replace() to place intStr // in the string buffer. sb.replace(0, intStr.length(), intStr); // output string buffer as a string System.out.print(sb.toString());

6 Program 8.2 (concluded) // every 10 elements output a newline
if(count % 10 == 0) System.out.println(); count++; } < sieve() developed in the earlier discussion >

7 Program 8.2 (Run) Run:

8 Student Question Why use a StringBuffer to build up the result String? Can’t a regular String be used instead? Why is a StringBuffer class even needed in Java?

9 Student Question The sieve method given in the textbook is not very efficient since it would not be necessary to remove multiples of 4 if in a previous step you have already removed multiples of 2. Show in pseudocode how the algorithm can be improved. Would you be able to take advantage of this improvement in Java using the Bag collection? If not, what collection would you need?


Download ppt "Sieve of Eratosthenes The Sieve of Eratosthenes uses a bag to find all primes less than or equal to an integer value n. Begin by creating a bag an inserting."

Similar presentations


Ads by Google