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.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
ADSA: Collections/ Advanced Data Structures and Algorithms Objective –give an overview of Collection classes, and create/use a Bag class Semester.
Introduction to Computer Programming Counting Loops.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Topic 25 - more array algorithms 1 "To excel in Java, or any computer language, you want to build skill in both the "large" and "small". By "large" I mean.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Oops... The last example of the last lecture: private static void makeEx (Ex e) { e = new Ex(); e.field = 4; } public static void main (String[] args)
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
CS1101: Programming Methodology Preparing for Practical Exam (PE)
AP Java Java’s version of Repeat until.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Introduction to Computer Science What is Computer Science? Getting Started Programming.
Parameter Passing: Arrays 1.Create new variables (boxes) for each of the formal parameters allocated on a fresh stack created for this function call. int.
Arrays Chapter 7.
Linked Data Structures
C++ Memory Management – Homework Exercises
Sixth Lecture ArrayList Abstract Class and Interface
Building Java Programs
Arrays 3/4 By Pius Nyaanga.
using System; namespace Demo01 { class Program
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
Chapter 8 – Arrays and Array Lists
Strings.
Towers of Hanoi Move n (4) disks from pole A to pole C
Sieve of Eratosthenes.
Using The Sieve of Eratosthenes
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Sieve of Eratosthenes.
OPERATORS (1) CSC 111.
Writing Methods.
Chapter 7: Strings and Characters
An Introduction to Java – Part I, language basics
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Recursion Recursion is a math and programming tool
AP Java Warm-up Boolean Array.
CSC 216/001 Lecture 1.
CS2013 Lecture 4 John Hurley Cal State LA.
Java Lesson 36 Mr. Kalmes.
Other displays Saving Arrays Using fors to process
CS2011 Introduction to Programming I Arrays (II)
class PrintOnetoTen { public static void main(String args[]) {
slides created by Ethan Apter
Recursion Problems.
Stacks.
CSE 143 Lecture 2 ArrayIntList, binary search
Common Array Algorithms
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Introduction to Algorithms
Lecture 22: Number Systems
Topic 25 - more array algorithms
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
CS 1054 Introduction to Programming in Java
Introduction to java Part I By Shenglan Zhang.
Introduction to Pointers
Presentation transcript:

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.

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++)

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;

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

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

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

Program 8.2 (Run) Run: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499

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?

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?