1 / 12

Sieve of Eratosthenes

Sieve of Eratosthenes. Lecture L7.2. Sieve of Eratosthenes in Java. package com.research.hanna; import com.ajile.drivers.gptc.TimerCounter; /** * @author Darrin Hanna */ public class Sieve { private static int j, a, b; private static int primeCount;

freira
Download Presentation

Sieve of Eratosthenes

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 of Eratosthenes Lecture L7.2

  2. Sieve of Eratosthenes in Java package com.research.hanna; import com.ajile.drivers.gptc.TimerCounter; /** * @author 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;

  3. 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( 5000000 ); tc.setMasterTimerEnabled( true );

  4. // 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; } } }

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

  6. Sieve of Eratosthenes in Forth : 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 ;

  7. : sieve ( -- n ) \ n = no. of prime #s 0 1024 1 FILL \ fill flags array 0 \ cnt 1024 FOR \ cnt 1024 R@ - \ cnt j DUP C@ \ 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 ;

  8. Flowpath datapath : sieve ( -- n ) \ n = no. of prime #s 0 1024 1 FILL \ fill flags array 0 \ cnt 1024 FOR \ cnt 1024 R@ - \ cnt j DUP C@ \ 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 ;

  9. Flowpath controller : sieve ( -- n ) \ n = no. of prime #s 0 1024 1 FILL \ fill flags array 0 \ cnt 1024 FOR \ cnt 1024 R@ - \ cnt j DUP C@ \ 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 ;

  10. FPGA-VHDL datapath

  11. FPGA-VHDL controller

More Related