Instantly share code, notes, and snippets.

# paxswill/fizzbuzz-recursive-bit.c Last active Sep 25, 2015

FizzBuzz with recursion and no multiplication or division
 #include #include int addDigitsHex(int num); void recursiveFizzBuzz(int start, int end); int main (int argc, char const *argv[]) { recursiveFizzBuzz(1, 100); return 0; } int addDigitsHex(int num) { // Add up each hex digit, resulting in a single hex digit sum // Ex: 111 => 3 // Ex: FC3 => 1E => F int workingNum = num; int accum = 0; while(workingNum > 0){ int temp = workingNum & 0xF; accum += temp; workingNum >>= 4; } if(accum > 0xF){ return addDigitsHex(accum); }else{ return accum; } } void recursiveFizzBuzz(int start, int limit) { if(start <= limit){ switch(addDigitsHex(start)){ //if divisible by 5, If you add up all of the hex digits, it will equal 5, A, or F case 0x5: case 0xA: printf("Buzz\n"); break; //if divisible by 3, If you add up all of the hex digits, it will equal 3, 6, 9, C or F case 0x3: case 0x6: case 0x9: printf("Fizz\n"); break; // F is the only intersection between the possibilities of divisible by 3 and 5 case 0xF: printf("FizzBuzz\n"); break; default: printf("%d\n", start); break; } recursiveFizzBuzz(++start, limit); } }