Sieve of Eratosthenes Lecture L7.2. Sieve of Eratosthenes in Java package com.research.hanna; import com.ajile.drivers.gptc.TimerCounter; /**

Slides:



Advertisements
Similar presentations
Sorting algorithms Sieve of Eratosthenes
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
CSCI 160 Midterm Review Rasanjalee DM.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
EC-241 Object-Oriented Programming
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Java Flowpaths: Efficiently Generating Circuits for Embedded Systems from Java WorldComp ESA 2006 Las Vegas, Nevada EXCERPT Darrin Hanna, Michael DuChene,
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
Chapter 4 Number Theory. Terms Factors Divides, divisible, divisibility Multiple.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Sieve public class sieve { public static void main(String args[]) {
Sieve of Eratosthenes. The Sieve of Eratosthenes is a method that.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
Multidimensional Arrays. 2 Two Dimensional Arrays Two dimensional arrays. n Table of grades for each student on various exams. n Table of data for each.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Object-Oriented Programming Simple Stack Implementation.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Take out a piece of paper and PEN. The quiz starts ONE minute after the tardy bell rings. You will have 45 – 90 seconds per question. Determine the output.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Computer Science 320 A First Program in Parallel Java.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CSCI-383 Object-Oriented Programming & Design Lecture 30.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
Prime Numbers Lecture L6.1 Sieve of Eratosthenes.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Prime Numbers Lecture L4.4 Sieve of Eratosthenes.
Information and Computer Sciences University of Hawaii, Manoa
CSC111 Quick Revision.
using System; namespace Demo01 { class Program
Lecture 2: Data Types, Variables, Operators, and Expressions
Control Structures.
Multiple if-else boolean Data
Using The Sieve of Eratosthenes
Section 5.7 Concurrency, Interference, and Synchronization.
Computer Programming Methodology Input and While Loop
TK1114 Computer Programming
null, true, and false are also reserved.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
In this class, we will cover:
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
AP Java Warm-up Boolean Array.
Multiple if-else boolean Data
Recursive GCD Demo public class Euclid {
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes – Week 4 Chapter 5 (Loops).
Java Programming with Multiple Classes
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.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
In this class, we will cover:
Building Java Programs
Lecture 22: Number Systems
Presentation transcript:

Sieve of Eratosthenes Lecture L7.2

Sieve of Eratosthenes in Java package com.research.hanna; import com.ajile.drivers.gptc.TimerCounter; /** Darrin Hanna */ public class Sieve { private static int j, a, b; private static int primeCount; private static int flags[] = new int[1024]; private static int size; private static TimerCounter tc; public static void main(String[] args) { int count2; size = 1024;

TimerCounter.setPrescalerClockSource( TimerCounter.INTERNAL_PERIPHERAL_CLOCK ); // Assuming Internal Peripheral clock is 50MHz, // then Prescaler clock out is 1MHz TimerCounter.setPrescalerReloadRegisterValue( 50 ); TimerCounter.setPrescalerEnabled( true ); // setup timer 0 tc = new TimerCounter( 0 ); //tc.setMode_IO_Line_B( TimerCounter.TIMER_0_OUTPUT_DIVIDE_BY_2 ); tc.setExternalTimerEnableMode( TimerCounter.TIMER_ENABLED_ONLY_VIA_MTEN_AND_TRIGGER ); System.out.println("timer start at: " + tc.getCurrentTimeRegisterValue()); tc.setReloadRegisterValue( ); tc.setMasterTimerEnabled( true );

// first create an array of flags, where // each member of the array stands for a odd number // starting with 3. for (j = 0; j < size; j++) { flags[j] = 1; } primeCount = 0; for (j = 0; j < size; j++) { if (flags[j] == 1) { // found a prime, count it primeCount++; // now set all of the multiples of // the current prime number to false // because they couldn't possibly be // prime a = 2*j + 3;// odd numbers starting with 3 b = a + j; while(b < size) { flags[b] = 0; b = b + a; }

count2 = tc.getCurrentTimeRegisterValue(); // output the number of primes System.out.println("Number of primes = " + primeCount); System.out.println("count2: " + count2); //Processor clock is MHz }

: FILL ( b u c -- ) \ fill u bytes at addr b with char c -ROT\ c b u FOR\ c b OVER OVER \ c b c b C! 1+\ c b+1 NEXT DROP DROP ; Sieve of Eratosthenes in Forth

: sieve ( -- n ) \ n = no. of prime #s FILL \ fill flags array 0 \ cnt 1024 FOR \ cnt \ cnt j DUP \ cnt j flags[j] IF \ cnt j DUP 2* 3 + \ cnt j a = (2*j + 3) TUCK + \ cnt a b = (a + j) BEGIN \ cnt a b DUP 1024 < \ cnt a b f WHILE \ cnt a b 0 OVER C! \ store 0 at flags[b] OVER + \ cnt a b = (a + b) REPEAT 2DROP 1+ \ cnt = cnt+1 ELSE DROP\ cnt THEN NEXT ;

Flowpath datapath : sieve ( -- n ) \ n = no. of prime #s FILL \ fill flags array 0 \ cnt 1024 FOR \ cnt \ cnt j DUP \ cnt j flags[j] IF \ cnt j DUP 2* 3 + \ cnt j a = (2*j + 3) TUCK + \ cnt a b = (a + j) BEGIN \ cnt a b DUP 1024 < \ cnt a b f WHILE \ cnt a b 0 OVER C! \ store 0 at flags[b] OVER + \ cnt a b = (a + b) REPEAT 2DROP 1+ \ cnt = cnt+1 ELSE DROP\ cnt THEN NEXT ;

Flowpath controller : sieve ( -- n ) \ n = no. of prime #s FILL \ fill flags array 0 \ cnt 1024 FOR \ cnt \ cnt j DUP \ cnt j flags[j] IF \ cnt j DUP 2* 3 + \ cnt j a = (2*j + 3) TUCK + \ cnt a b = (a + j) BEGIN \ cnt a b DUP 1024 < \ cnt a b f WHILE \ cnt a b 0 OVER C! \ store 0 at flags[b] OVER + \ cnt a b = (a + b) REPEAT 2DROP 1+ \ cnt = cnt+1 ELSE DROP\ cnt THEN NEXT ;

FPGA-VHDL datapath

FPGA-VHDL controller