Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Tuesday, February 7, 2012

#11 - Define Recursion: See Recursion

There is one last principal form of repetition remaining to be discussed. This form of repetition is the bane of many first year Computer Science majors and dreaded only more by the professors and TAs who must answer these questions that are best paraphrased as "I just don't get it." That principle? Recursion. I have found no more precise and snarky answer than found in this stackoverflow answer:
If you understand recursion, you're done. Otherwise go read this StackOverflow question...
The link, of course, is back to the very same question.
public class FizzBuzz11 {

  public static void main(String[] arg) {
    recurse(100);
  }

  public static void recurse(int num) {
    if (num > 0) {
      recurse(num - 1);
      if (num % 15 == 0) {
        System.out.println("FizzBuzz");
      } else if (num % 5 == 0) {
        System.out.println("Buzz");
      } else if (num % 3 == 0) {
        System.out.println("Fizz");
      } else {
        System.out.println(num);
      }
    }
  }
}

There are no control structures traditionally associated with repetition in this sample.  No for loops of any stripe, no while or do/while.  Not even a GOTO statement (that would be harmful). What does do the repetition is the call to the recurse method at line 9. That line, of course, is in the very method that it is calling. A snake eating it's own tail.

Of course, a snake will never be able to fully consume itself.  This is assuming it could get over the pain of biting it's own tail, which is about as bad as the anguish felt by many students experiencing their first semester of programming.   At some point things will either hurt too much or the snake's stomach will get too full.  This is called the "base case" where a simple answer is known and we can unwind the stack.

There are a lot of really neat intricacies that can be used with recursion, but this task is much to simple to demonstrate them in this form.  The form used for this recursion is to count backwards (line 10) from the last answer needed (line 4), stop when we hit zero (line 9), and when unwinding the stack calculate the answer for the number at hand (lines 11 to 18).  There is no work done both before and after the recursion, only after.  There is no multiple winding and unwinding of the stack, just one dive in and out.  And there is no use of the returned values from the recursion, in fact there is no return value at all.

All of this aligns with my experience with recursion seen 'in the field.'  Simple recursion can be handled by anyone deserving to be paid market rates.  But you throw in the more interesting cases (as a computer scientist would consider interesting) you are better off shipping it in a binary compiled format.  Because if the typical developer is given a chance to mess with the source code, they will break something.  Until the point they suddenly understand it.

No comments:

Post a Comment