Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* FizzBuzz implemented with a sieve */
- #include <stdio.h>
- #include <malloc.h>
- #define FLAG_DIV_3 1
- #define FLAG_DIV_5 2
- int
- main(int argc, char**argv)
- {
- char *sieve;
- int count;
- int sloop;
- if (argc >1)
- count = atoi(argv[1]) + 1; /* allow for indexed array */
- else
- count = 101;
- sieve = calloc(count, 1);
- /* mark values in sieve */
- for (sloop = 3; sloop < count; sloop+= 3) {
- sieve[sloop] |= FLAG_DIV_3;
- }
- for (sloop = 5; sloop < count; sloop+= 5) {
- sieve[sloop] |= FLAG_DIV_5;
- }
- /* output */
- for (sloop = 1; sloop < count; sloop++) {
- if (sieve[sloop] == 0) {
- printf("%d", sloop);
- }
- if ((sieve[sloop] & FLAG_DIV_3) != 0) {
- printf("Fizz");
- }
- if ((sieve[sloop] & FLAG_DIV_5) != 0) {
- printf("Buzz");
- }
- printf(" ");
- }
- free(sieve);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement