Code archives/Algorithms/FAST Sieve of Eratosthenes
This code has been declared by its author to be Public Domain code.
Download source code
| Find prime numbers smaller than 16,000,000 in a flash. (Less than 2 seconds on a 1Ghz system) Option to print the primes to screen included. Do NOT print out the prime numbers greater than 50,000 unless you want to tie up your computer for a long time. Suggest writing to text file for large primes. |
;Fast Sieve of Eratosthenes ;Andy Amaya ;Nov 16,2006 Print "Sieve of Eratosthenes" Print "" limit% = 16000000 ;Sixteen million If limit >= 16000000 Then limit = 16000000 st% = MilliSecs() Dim prime%(limit) prime(1) = 1 For n% = 4 To limit Step 2 prime(n) = 1 Next For n = 3 To Sqr(limit) Step 2 If prime(n) = 0 Then inc% = n+n i% = n*n While i <= limit prime(i) = 1 i = i + inc Wend End If Next et = MilliSecs()-st For n = 1 To limit If prime(n) = 0 Then pcount% = pcount + 1 Next ;================================================ ; set showPrimes to 1 to print out primes in rows showPrimes% = 0 numPerRow% = 10 ;ten primes per row ;************************************************ ;******* DO NOT USE WITH LARGE NUMBERS! ******* ;************************************************ ;================================================ If showPrimes = 1 Then count = 1 For n = 1 To limit If prime(n) = 0 Then count = count + 1 If count <= numPerRow Then temp$ = temp$ + Str(n)+", " Else temp$ = temp$ + Str(n) Print temp$ temp$ = "" count = 1 End If End If Next If count < numPerRow Then Print temp$ End If End If ;================================================ Print "There are "+pcount+" primes between 1 and "+limit Print "" Print "ET = "+et+" milliseconds" Print "" a$ = Input("Press [Enter] to Exit") |