The Basics of Recursion vs Iteration

Today’s topic was chosen by Prad N, who attends UT Austin. The original topic as proposed was “recursion vs iteration with stacks”; however, that is a fairly narrow—and somewhat technical—topic, so this blog post will discuss recursion and plain old iteration.

First off, recursion and iteration are terms used in computer science. Iteration, essentially, is a process by which you count something or carry out a number of steps, in order. For instance, if you want to write a program that subtracts one dollar from every bank account in the world and adds it to your own, you would be illicitly iterating through a bunch of bank accounts.

Recursion is a process by which something refers to itself, such as this sentence.

That would be called a recursive sentence, because of the phrase “this sentence.” In logic, recursion can lead to paradoxes, such as “This sentence is false.” Anyways…

In computer programming, recursion is when a function calls itself. Here’s an example. Say you write a program that has three steps, A, B, and then C. If the program just executes them in order, it would be iterative. But suppose in step A, one of the things to do is to run step A. That is recursion.

But wait, you say! If one of the things to do in step A is to do step A, wouldn’t that require another run of step A, and wouldn’t that one as well, etc.? If done poorly, recursion can do this. The program would never end.

For a real example, consider the mathematical factorial function,

n! = n(n-1)(n-2)\cdots(3)(2)(1)

There is a recursive way to define the function, namely that

n! = n\cdot(n-1)!

There is something missing here, however, and it is that it has no endpoint. If we wanted to figure out 3! using this method, we find that it is equal to 3*2! = 3*2*1! = 3*2*1*0! = …. Of course, we want to make it clear that 1! = 1. So the recursive definition must have both a formula AND a way to end:

n! = n\cdot(n-1)!, 1! = 1

In Java, we would have something like this:

int factorial(int n) {
  if (n==1) return 1;
  return n * factorial(n-1);

Here’s what it’s doing in English. Suppose you input the number 4.  It will run the line starting with “if”, and because 4 does not equal 1, it will move on to the next line. This tells the function to return n * factorial(n-1), which is, in this case, 4 * factorial(3). But factorial(3) is just 3 * factorial(2), Using substitution, we have factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2). Continuing on, we have factorial(2) = 2 * factorial(1). Here is the ending step, because factorial(1) will trigger the “if” condition and cause it to run “return 1” and skip the recursive line. So 4 * 3 * factorial(2) = 4 * 3 * 2 * factorial(1) = 4 * 3 * 2 * 1. At this point, the computer will just calculate the product and spit out 24.

Of course, one can do it iteratively as well:

int factorial(int n) {
  int i = 1;
  int product = 1;
  while(i < n) {
    i = i + 1;
    product = product * i;
  return product;

I won’t explain it in full this time. The idea is that it multiplies all the integers from 1 up to the number you input. The two algorithms do essentially the same thing, but one does it by calling itself and other other by running a loop. Recursion is usually more fun.

Going Viral: Planning vs. the Internet

Almost a month ago, on November 18, I was stumbling on the Internet, taking a break from novel-writing for NaNoWriMo. I was in the Olin Library, at Cornell University. Suddenly I had the idea to make a diagram of how I waste a lot of time on the Internet. That became the now viral “Planning vs. the Internet” image (here’s the original post):

The only software I used were Microsoft Publisher (I just created a very long, blank sheet), Paint, and GIMP. The most painful part was actually getting all the icons and aligning things together.

I was first notified of its viral factor when the viewcount on my blog spiked. It wasn’t very much. The daily views went from an average of 150 suddenly to 600. I have experienced a really big spike (80,000 in one day) in the past, but I knew there was something weird here, because I tracked a relatively large number of hits on the home page, not on any post in particular. I wasn’t really sure what to make of this.

Then two real-life friends from two different places messaged me on Facebook, saying that they had stumbled upon this image. I immediately did a google search for *planning vs the internet* but found only a few websites that had uploaded the image, and so were gaining viewcount for themselves. This made sense. The only identification on this image is the blurb I put on the bottom right corner, which  goes to my homepage, so it would make sense that I received a homepage spike. But of all the websites I found put together, plus this site itself, I counted at most 2000 views. It was too coincidental that two of these 2000 would be people I knew in real life. There must be views coming from something else, something that wasn’t stat tracked. This was all a few days prior to this post.

Then just an hour ago, I found it. THIS.

That was it. A huge spike from StumbleUpon. 269K views at the time of this post. But the url stumbled is the image file itself, not the post in which I put up the image. So WordPress is actually not keeping track of any of these 269K hits. That’s why, on the right-hand bar, it says 230,367 views (at the time of the current post), which is lower than the amount on the image alone. Too bad it doesn’t count. Had it counted, it would have over doubled the total number of hits on this blog.

Some trivia about the image:

  • that Wikipedia featured article was the Nov. 18 one, “City of Blinding Lights
  • the Cracked article is from Nov. 17, “5 Minor Screw-ups that Created the Modern World
  • in the WordPress screenshot, the graph is actually the graph of this blog’s stats. Gray bars are weekends. Note that Sunday consistently got more views than Saturday.
  • in the Tumblr screenshot, the text says, “I have said this many times in my head and even a couple of times out loud, but I will say it only once here:” The next line, which is partially cut off, says, “Fuck Internet Explorer.” It looks different in the link because the screenshot on the image is from the Tumblr dashboard. The author behind the quote is Karan, the same one who instigated this post.
  • in the gmail screenshot, the two people are Stephanie Richmond and Lauren Mangano, and both emails were band-related. The top one says, “Rehearsal Tomorrow & Senior Concert reminder” and the other, “Penn Band Arrival Time.” If you read very carefully, you’ll see that both start with “Hey Band.” Stephanie is the drum major and Lauren is the head manager. Cornell Big Red Marching Band for the win!
  • for English, I am in English 1170, or Short Stories. We normally write two discussion questions over texts that we read. Blackboard is a site used by many schools to organize academic stuff.
  • on that Sociology exam that I referred to, I got an A+. 😛
  • the 3 webcomics which have some iconic character on the picture are Cyanide and Happinessxkcd, and Abstruse Goose.
  • StumbleUpon is an awesome but addicting site.

*You can click the image to enlarge it if you wish to follow along with the trivia.