home *** CD-ROM | disk | FTP | other *** search
- /*
- // A simple demonstration of the Turbo C++ 1.0 stream I/O capabilities
- // using an interactive version of the Sieve benchmark.
- */
-
- #include <stdio.h>
-
- #define TRUE 1
- #define FALSE 0
- #define SIZE 8191
-
- char flags[ SIZE + 1 ] ;
-
- void main()
- {
- register int i, k, count ;
- unsigned limit ;
- char buf[20] ;
-
- /* Get the number of iterations */
- printf("\nSieve upper limit? ") ;
- while (1) {
- gets(buf) ;
- if (sscanf(buf, "%u", &limit) == 1) {
- if (limit >= 2 && limit <= SIZE)
- break ;
- }
- printf("Enter a number between 2 and %u: ", SIZE) ;
- }
-
- /* Perform the initialization */
- count = 0 ;
- for (i = 0; i <= limit; i++)
- flags[i] = TRUE ;
-
- /* Run the Sieve */
- for (i = 2; i <= limit; i++) {
- if (flags[i]) {
- /* Cancel out all multiples of this prime */
- for (k = i + i; k <= limit; k += i)
- flags[k] = FALSE ;
- count++ ;
- }
- }
-
- /* Display the count of primes */
- printf("%u primes found between 2 and %u\n", count, limit) ;
- }