4 ms·
Let's optimize the if statements away, too (no extra memory accesses or branches, either). #include <stdio.h> #include <stdint.h> #define F(n) (1 << (
by rbehrends 9y ago
Let's optimize the if statements away, too (no extra memory accesses or branches, either).
#include <stdio.h>
#include <stdint.h>
#define F(n) (1 << (2*(n)-2))
#define B(n) (1 << (2*(n)-1))
int main() {
uint32_t m = F(3) | F(6) | F(9) | F(12) | F(15) | B(5) | B(10) | B(15);
int i;
static char t[4][16]= { "%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n" };
for (i = 1; i <= 100; i++) {
printf(t[m&3], i);
m = (m >> 2) | (m << 28);
}
return 0;
}
Transforming that into an if-based version is left as an exercise for the reader. :)