home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / STARTUP.ZIP / EXAMPLES / STDIO / STDIO.C < prev   
Encoding:
C/C++ Source or Header  |  1990-09-01  |  1021 b   |  49 lines

  1. /*
  2. //        A simple demonstration of the Turbo C++ 1.0 stream I/O capabilities
  3. //        using an interactive version of the Sieve benchmark.
  4. */
  5.  
  6. #include    <stdio.h>
  7.  
  8. #define    TRUE            1
  9. #define    FALSE            0
  10. #define    SIZE            8191
  11.  
  12. char    flags[ SIZE + 1 ] ;
  13.  
  14. void        main()
  15. {
  16.     register int    i, k, count ;
  17.     unsigned    limit ;
  18.     char        buf[20] ;
  19.  
  20.     /* Get the number of iterations */
  21.     printf("\nSieve upper limit? ") ;
  22.     while (1)   {
  23.         gets(buf) ;
  24.         if (sscanf(buf, "%u", &limit) == 1)   {
  25.             if (limit >= 2 && limit <= SIZE)
  26.                 break ;
  27.         }
  28.         printf("Enter a number between 2 and %u: ", SIZE) ;
  29.     }
  30.  
  31.     /* Perform the initialization */
  32.     count = 0 ;
  33.     for (i = 0; i <= limit; i++)
  34.         flags[i] = TRUE ;
  35.  
  36.     /* Run the Sieve */
  37.     for (i = 2; i <= limit; i++)   {
  38.         if (flags[i])   {
  39.             /* Cancel out all multiples of this prime */
  40.             for (k = i + i; k <= limit; k += i)
  41.                 flags[k] = FALSE ;
  42.             count++ ;
  43.         }
  44.     }
  45.  
  46.     /* Display the count of primes */
  47.     printf("%u primes found between 2 and %u\n", count, limit) ;
  48. }
  49.