Skip to content

Instantly share code, notes, and snippets.

@paxswill
Last active September 25, 2015 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paxswill/952919 to your computer and use it in GitHub Desktop.
Save paxswill/952919 to your computer and use it in GitHub Desktop.
FizzBuzz with recursion and no multiplication or division
#include <stdio.h>
#include <stdbool.h>
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment