Sieve of Eratosthenes Lecture L 7 2 Sieve












- Slides: 12
Sieve of Eratosthenes Lecture L 7. 2
Sieve of Eratosthenes in Java package com. research. hanna; import com. ajile. drivers. gptc. Timer. Counter; /** * @author Darrin Hanna */ public class Sieve { private private static static int j, a, b; int prime. Count; int flags[] = new int[1024]; int size; Timer. Counter tc; public static void main(String[] args) { int count 2; size = 1024;
Timer. Counter. set. Prescaler. Clock. Source( Timer. Counter. INTERNAL_PERIPHERAL_CLOCK ); // Assuming Internal Peripheral clock is 50 MHz, // then Prescaler clock out is 1 MHz Timer. Counter. set. Prescaler. Reload. Register. Value( 50 ); Timer. Counter. set. Prescaler. Enabled( true ); // setup timer 0 tc = new Timer. Counter( 0 ); //tc. set. Mode_IO_Line_B( Timer. Counter. TIMER_0_OUTPUT_DIVIDE_BY_2 ); tc. set. External. Timer. Enable. Mode( Timer. Counter. TIMER_ENABLED_ONLY_VIA_MTEN_AND_TRIGGER ); System. out. println("timer start at: " + tc. get. Current. Time. Register. Value()); tc. set. Reload. Register. Value( 5000000 ); tc. set. Master. Timer. Enabled( 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; } prime. Count = 0; for (j = 0; j < size; j++) { if (flags[j] == 1) { // found a prime, count it prime. Count++; } } // 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; }
count 2 = tc. get. Current. Time. Register. Value(); // output the number of primes System. out. println("Number of primes = " + prime. Count); System. out. println("count 2: " + count 2); //Processor clock is 73. 728 MHz } }
Sieve of Eratosthenes in Forth : FILL ( b u c -- ) fill u bytes at addr -ROT FOR OVER C! 1+ NEXT DROP ; b with char c c b u c b c b+1
: sieve ( -- n ) 0 1024 1 FILL 0 1024 FOR 1024 R@ DUP C@ IF DUP 2* 3 + TUCK + BEGIN DUP 1024 < WHILE 0 OVER C! OVER + REPEAT 2 DROP 1+ ELSE DROP THEN NEXT ; n = no. of prime #s fill flags array cnt cnt j flags[j] cnt j a = (2*j + 3) cnt a b = (a + j) cnt a b f cnt a b store 0 at flags[b] cnt a b = (a + b) cnt = cnt+1 cnt
Flowpath datapath : sieve ( -- n ) 0 1024 1 FILL 0 1024 FOR 1024 R@ DUP C@ IF DUP 2* 3 + TUCK + BEGIN DUP 1024 < WHILE 0 OVER C! OVER + REPEAT 2 DROP 1+ ELSE DROP THEN NEXT ; n = no. of prime #s fill flags array cnt j flags[j] cnt j a = (2*j + 3) cnt a b = (a + j) cnt a b f cnt a b store 0 at flags[b] cnt a b = (a + b) cnt = cnt+1 cnt
Flowpath controller : sieve ( -- n ) 0 1024 1 FILL 0 1024 FOR 1024 R@ DUP C@ IF DUP 2* 3 + TUCK + BEGIN DUP 1024 < WHILE 0 OVER C! OVER + REPEAT 2 DROP 1+ ELSE DROP THEN NEXT ; n = no. of prime #s fill flags array cnt j flags[j] cnt j a = (2*j + 3) cnt a b = (a + j) cnt a b f cnt a b store 0 at flags[b] cnt a b = (a + b) cnt = cnt+1 cnt
FPGA-VHDL datapath
FPGA-VHDL controller