Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 // 00002 // Title : Sieve of Eratosthenes 00003 // Author : Claus Kuehnel 00004 // Date : 2016-02-16 00005 // Id : sieve_ARCH_PRO 00006 // based on : sieve.pde from 2010 00007 // 00008 // DISCLAIMER: 00009 // The author is in no way responsible for any problems or damage caused by 00010 // using this code. Use at your own risk. 00011 // 00012 // LICENSE: 00013 // This code is distributed under the GNU Public License 00014 // which can be found at http://www.gnu.org/licenses/gpl.txt 00015 // 00016 00017 #include "mbed.h" 00018 00019 #define TRUE 1 00020 #define FALSE 0 00021 00022 int i,k, prime,count; 00023 00024 const int SIZE = 1000; 00025 char flags[SIZE+1]; 00026 00027 Serial pc(USBTX, USBRX); 00028 Timer timer; 00029 00030 DigitalOut myled(LED1); 00031 00032 int main() 00033 { 00034 myled = 0; 00035 pc.baud(115200); 00036 pc.printf("Sieve of Eratosthenes - Arch Pro\n"); 00037 00038 /*------------------------------------------------------------------- 00039 The following code is an implementation of the Sieve of Eratosthenes. 00040 -------------------------------------------------------------------*/ 00041 pc.printf("5000 iterations\n"); 00042 timer.start(); 00043 unsigned long time = timer.read_ms(); 00044 for (unsigned int iter = 1; iter <= 5000; iter++) /* do program 5000 times */ 00045 { 00046 count = 0; /* initialize prime counter */ 00047 for (i = 0; i <= SIZE; i++) /* set all flags true */ 00048 flags[i] = TRUE; 00049 for (i = 0; i <= SIZE; i++) 00050 { 00051 if (flags[i]) /* found a prime */ 00052 { 00053 prime = i + i + 3; /* twice index + 3 */ 00054 for (k = i + prime; k <= SIZE; k += prime) 00055 flags[k] = FALSE; /* kill all multiples */ 00056 count++; /* primes found */ 00057 } 00058 } 00059 } 00060 myled = 1; 00061 time = timer.read_ms() - time; 00062 pc.printf("%d primes.\n", count); 00063 pc.printf("Runtime = %d ms\n",time); 00064 }
Generated on Thu Jul 28 2022 23:46:36 by
1.7.2