1 / 8

Sieve

Sieve. public class sieve { public static void main(String args[]) { int max = 100; boolean prime[] = new boolean[max+1]; int n,m; // initialize the array (entries 0,1 unused) for (n = 2; n <= max; n++) { prime[n] = true;

lars
Download Presentation

Sieve

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sieve public class sieve { public static void main(String args[]) { int max = 100; boolean prime[] = new boolean[max+1]; int n,m; // initialize the array (entries 0,1 unused) for (n = 2; n <= max; n++) { prime[n] = true; } // cross out multiples of uncrossed numbers for (n = 2; n <= max; n++) { if (prime[n]) { for (m = 2*n; m <= max; m += n) { prime[m] = false; } } } // print out primes: uncrossed numbers for (n = 2; n <= max; n++) { if (prime[n]) { System.out.print(n+" "); } } System.out.println(); } }

  2. Queue class Queue { char q[]; // this array holds the queue int putloc, getloc; // the put and get indices Queue(int size) { q = new char[size+1]; // allocate memory for queue putloc = getloc = 0; } boolean isEmpty(){ if (getloc == putloc) return true; else return false; } boolean isFull(){ if (putloc == q.length-1) return true; else return false; }

  3. // put a characer into the queue void put(char ch) { if (isFull()) { System.out.println(" -- Queue is full."); return; } putloc++; q[putloc] = ch; } // get a character from the queue char get() { if (isEmpty()){ System.out.println(" -- Queue is empty."); return (char) 0; } getloc++; return q[getloc]; } }

  4. // Demonstrate the Queue class. class QueueDemo { public static void main(String args[]) { Queue bigQ = new Queue(100); Queue smallQ = new Queue(4); char ch; int i; System.out.println("Using bigQ to store the alphabet."); // put some numbers into bigQ for(i=0; i < 26; i++) bigQ.put((char) ('A' + i)); // retrieve and display elements from bigQ System.out.print("Contents of bigQ: "); for(i=0; i < 26; i++) { ch = bigQ.get(); if(ch != (char) 0) System.out.print(ch); } System.out.println("\n");

  5. System.out.println("Using smallQ to generate erros."); // Now, use smallQ to generate some errors for(i=0; i < 5; i++) { System.out.print("Attempting to store " + (char) ('Z' - i)); smallQ.put((char) ('Z' - i)); System.out.println(); } System.out.println(); // more errors on smallQ System.out.print("Contents of smallQ: "); for(i=0; i < 5; i++) { ch = smallQ.get(); if(ch != (char) 0) System.out.print(ch); } } }

  6. Stack class Stack { int st[]; // this array holds the stack int sp; // the stack pointer index Stack(int size) { st = new int[size+1]; // allocate memory for stack sp = 0; } boolean isFull(){ if (sp == (st.length-1)) return true; else return false; } boolean isEmpty(){ if (sp == 0) return true; else return false; }

  7. void push(int n) { if(isFull()) { System.out.println(" -- Stack is full."); return; } sp++; st[sp] = n; } int pop() { int top; if(isEmpty()) { System.out.println(" -- Stack is empty."); return -1000; } top = st[sp]; sp--; return top; } }

  8. // Demonstrate the Stack class. class StackDemo { public static void main(String args[]) { Stack st1 = new Stack(5); int i,n; System.out.println("Using stack st1 to store numbers."); // put some numbers into st1 for(i=1; i < 7; i++) st1.push(i); // pop and display elements from st1 System.out.println("Contents of st1: "); for(i=1; i < 7; i++) { n = st1.pop(); if (n != -1000) System.out.println(n); } } }

More Related