• Source
    1. // We have bunnies standing in a line, numbered 1, 2, ...
    2. // The odd bunnies (1, 3, ..) have the normal 2 ears.
    3. // The even bunnies (2, 4, ..) we'll say have 3 ears, because they each have a raised foot.
    4. // Recursively return the number of "ears" in the bunny line 1, 2, ... n
    5. // (without loops or multiplication).
    6. // bunnyEars2(0) → 0
    7. // bunnyEars2(1) → 2
    8. // bunnyEars2(2) → 5
    9.  
    10. #include <stdio.h>
    11.  
    12. // To enable debug messages uncomment #define
    13. #define TEST 1
    14.  
    15. int bunnyEars2(int bunnies);
    16. void startTesting();
    17.  
    18. int main(void) {
    19. #ifdef TEST
    20. startTesting();
    21. #endif
    22.  
    23. return 0;
    24. }
    25.  
    26. int bunnyEars2(int bunnies) {
    27. if (bunnies == 0) {
    28. return 0;
    29. }
    30. if (bunnies == 1) {
    31. return 2;
    32. }
    33.  
    34. if (bunnies % 2 == 0) {
    35. return 3 + bunnyEars2(bunnies - 1);
    36. }
    37.  
    38. return 2 + bunnyEars2(bunnies - 1);
    39. }
    40.  
    41. void startTesting()
    42. {
    43. int result = 0;
    44. int i = 0;
    45.  
    46. for (i = 0; i <= 5; i++) {
    47. result = bunnyEars2(i);
    48.  
    49. printf("BunnyEars(%d) = %d\n", i, result);
    50. }
    51. }