Friday, September 23, 2011

Project Euler - Problem One Redux

So it's been a while. School is...well...school is hell this semester. But hey! It's all good, right? Today I decided that I'm going to sit down and relax my mind by doing some easy stuff. I couldn't let myself be unproductive though, so I decided to try and do some computer science studying. Sooooooo, I redid project Euler's problem one.  Except this time I also used recursion. It's pretty cool looking, and I'll walk you through how it works.

Okay, so instead of putting a big ole block of code, I just took a screenshot. Project One is simple enough, and if you actually want a big block explaining exactly what's going on, check out my first post.  Instead of the code itself, I took a screenshot.

You can see pretty much everything there. I tried to be all studious and comment it nicely.

Now, that works pretty darn well. Recursion makes it even cooler looking though.

Recursion is used whenever you can see that the answer to a problem is really just a smaller version of the problem.  Now, my computer science professor did say that it's not always best to use recursion.  It takes a lot of stuff to make a recursive call, so it probably shouldn't be used for just everything you've got.  As it is though, this function is so small that it really doesn't matter. PLUS IT IS SO COOL.

So, how does it work?

It's as simple as a function that calls itself.  To get to that point though, we need to figure out what the simplest case is. In the implementation that I'm using, the reasoning goes like this:
  1. We'll pass in the current position (a default of one) and the upper limit (which is 1000 for Project Euler).
  2. The simplest case, the base case, is if these two numbers are equal to each other.  That would mean that the number we are currently working with is the upper limit.
  3. Otherwise, we want to find out whether the number we're currently working with is a multiple of three or five. 
    • We can do this using the modulus  (%)  operator. It is the remainder of division between two numbers. If number A is a multiple of number B, then A%B should be zero (since it should divide evenly, thus without a remainder).
  4. Once we've establised that a number IS a multiple of three or five, we want to add it to the total. 
  5. Now we want to do the same thing again, except on a number that is one higher than our current number.
So, in pseudo-code:
If the current number is equal to the upper limit, return from the function. Else, we want to check whether the current number modulus three or five is equal to zero. If so, then we know it is a multiple of either three or five and should therefore be added to the total, so we return it AND we call the function again.

Now, think over that and see what you get. Here's mine!



If you've got any questions, please ask! This isn't exactly a tutorial on how to program, but it should guide you into how to do at least this problem recursively.


No comments:

Post a Comment