Skip to content

Instantly share code, notes, and snippets.

@lfnoise
Last active January 16, 2021 02:36
Show Gist options
  • Save lfnoise/cf3deb38006458bb4c5f74ea4c27126a to your computer and use it in GitHub Desktop.
Save lfnoise/cf3deb38006458bb4c5f74ea4c27126a to your computer and use it in GitHub Desktop.
An implementation of fizzbuzz with only one modulo and one comparison per cycle.
//
// main.c
// fizzbuzz
//
// Created by James McCartney on 1/15/21.
//
#include <stdio.h>
void fizzbuzz(int n)
{
static const char* format[4] = {"%d\n", "fizz\n", "buzz\n", "fizzbuzz\n"};
for (int i = 0; i < n; ++i) {
int j = 0x3 & (0x01241843 >> ((i % 15) << 1));
printf(format[j], i);
}
}
int main(int argc, const char * argv[])
{
fizzbuzz(101);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment