home | |||
links |
|
||
developer's mag main page article part 1 part 2 part 3 part 4 part 5 part 6 part 7 part 8 part 9 |
2 - Assembly SieveOur program is a popular benchmark program, the Sieve of Eratosthenes. This prime selection method is very easy to learn and code: set up a list of numbers, start at 2, and cross off every multiple (4, 6, 8, etc). When you've reached the limit of the numbers you are checking, go back and continue with 3, and so on. If the next number is crossed off, simply skip it and move on. Eventually you are at the end, and the uncrossed numbers are all the primes. (An entertaining Java version of the Sieve in action is available online, and is worth a visit)Since the Sieve is so simple, we might be tempted to code it right away in C, resulting in something like the following: void PerformSieve(int *array,int limit) { // pass in array already cleared for (int i=2;i<limit;i++) { for (int k=i+i;k<limit;k+=i) array[k]=1; // mark item as composite (non-prime) } }Although this will work, it can benefit from some optimizing:
Previous Section Next Section |
||
Copyright © 2001-2006 ebmDevMag.com - Legal Notice |