|
|
|
|
|
by showdead
3218 days ago
|
|
Well, there is this approach: #include <stdio.h>
#include <stdint.h>
#define FIZZ(x) (x & 0x0924)
#define BUZZ(x) (x & 0x0210)
#define FIZZBUZZ(x) (x & 0x4000)
#define NONE(x) (x & 0x34cb)
void fizzbuzz(size_t n_max)
{
uint32_t x = 1;
for (size_t i = 1; i <= n_max; i++)
{
if (FIZZ(x))
printf("fizz\n");
if (BUZZ(x))
printf("buzz\n");
if (FIZZBUZZ(x))
printf("fizzbuzz\n");
if (NONE(x))
printf("%d\n", i);
x <<= 1;
x |= (x >> 15);
}
}
int main(int argc, char **argv)
{
fizzbuzz(100);
}
It's less costly than integer division, and I'm not sure it's what you had in mind. The output here still duplicates the strings. |
|
Congrats!