Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /* FizzBuzz implemented with a sieve */
  2.  
  3. #include <stdio.h>
  4. #include <malloc.h>
  5.  
  6. #define FLAG_DIV_3 1
  7. #define FLAG_DIV_5 2
  8.  
  9. int
  10. main(int argc, char**argv)
  11. {
  12.     char *sieve;
  13.     int count;
  14.     int sloop;
  15.  
  16.     if (argc >1)
  17.         count = atoi(argv[1]) + 1; /* allow for indexed array */
  18.     else
  19.         count = 101;
  20.  
  21.     sieve = calloc(count, 1);
  22.  
  23.         /* mark values in sieve */
  24.     for (sloop = 3; sloop < count; sloop+= 3) {
  25.         sieve[sloop] |= FLAG_DIV_3;
  26.     }
  27.  
  28.     for (sloop = 5; sloop < count; sloop+= 5) {
  29.         sieve[sloop] |= FLAG_DIV_5;
  30.     }
  31.  
  32.     /* output */
  33.     for (sloop = 1; sloop < count; sloop++) {
  34.         if (sieve[sloop] == 0) {
  35.             printf("%d", sloop);
  36.         }
  37.         if ((sieve[sloop] & FLAG_DIV_3) != 0) {
  38.             printf("Fizz");        
  39.         }
  40.         if ((sieve[sloop] & FLAG_DIV_5) != 0) {
  41.             printf("Buzz");
  42.            
  43.         }
  44.         printf(" ");
  45.     }
  46.  
  47.     free(sieve);
  48.  
  49.     return 0;
  50. }