Yes the patterns repeat every 15 integers.
So you only need to do one division operation, get the index modulo-15.
I was bored once at work and figured out a way to compute this without doing much arithmetic. It only requires 1 modulo operation.
for (int i=1; i<100;i++)
{
// make a bit vector with a 1 in ith bit, modulo 15.
unsigned int i_as_bitvector = 1 << (i % 15);
// Check it against the valid positions for FizzBuzz
// 0ctal 011111 is binary 1001001001001
// Octal 02041 is binary 10000100001
printf("%d ", i);
if (i_as_bitvector & 011111 ) printf("Fizz");
if (i_as_bitvector & 02041 ) printf("Buzz");
printf("\n");
}
I also have a version which has NO magic constants anywhere in the program, except 3 and 5. I'll post it if someone is interested.
I was bored once at work and figured out a way to compute this without doing much arithmetic. It only requires 1 modulo operation.
I also have a version which has NO magic constants anywhere in the program, except 3 and 5. I'll post it if someone is interested.