Problem 2: By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Commentary: This problem again is not very tricky. It is worth mentioning here that I am a mathematician by training, and I’m about to math the hell out. If you’re not into that, you are encouraged to skip this entire section.

The Fibonacci numbers are governed by the recurrence relation:

with . What’s not stated in the Project Euler explanation (which will become debatably useful in a later problem), is that there is a “closed form” to the Fibonacci Numbers, given by:

(where the exponents “” would be replaced by if you index the sequence via ). Demonstrating this is not particularly difficult if you’re at all comfortable with simple calculus (which now qualifies as high school mathematics). To do this, we’ll use a generating function (there are other approaches to get this “closed form”, but this, in my opinion, is by far the simplest). Let’s first construct a funny thing called :

You might be wondering if this is a sensible definition, i.e., does it even make sense to say that this arcane construction even exists. If so, congratulations, you think like a mathematician–yes, you too could be a poor man some day! If you never bothered to wonder, then you lucked out, because that can ultimately be considered an irrelevant question in the first place. But even the staunchest pedant can be convinced that this is a reasonable construction–which is a discussion I don’t really feel like having here. Deal with it, nerd.

Ok, so weird thing in hand, let’s play with it. Notice that

and after re-kajiggering the indices (that’s the technical way to say it), and doing a little grouping, we have:

So that

Hence

The last bit just devolves into a bunch of boring arithmetic, so I’ll keep the details fairly simple from here on. Using the method of partial fractions decomposition, we can further write

and with only a little elbow grease, you can show that , , , and . Of course, these numbers are themselves quite well studied–typically is written as , and given the interesting name “the golden ratio” (more on that in a moment). So then

and by merely comparing coefficients among these two series, we have

which is exactly what we wanted to show!

Of course, the Golden Ratio has many cute properties and ties to the Fibonacci Numbers. For example,

Actually, as it turns out, this limit shows up more commonly than you might expect. But it does give some insight into the funny way some people define –in terms of its so-called continued fraction expansion. Starting with an example calculation, let’s look at and . Notice that

So you might be inclined to think that

And you would be right!

Now where were we? Oh right; that R thing. This closed form is easy enough to implement in R to give you the Fibonacci Number of your choice without having to compute all prior ones–but it’s not terribly efficient about it:

GetNthFib <- function(n){
  1/sqrt(5)*(((1+sqrt(5))/2)^{n+1} - ((1-sqrt(5))/2)^{n+1})
}

One final word before the R Project Euler solution: the Project Euler people use the indexing , and then define the recurrence relation for all . This is fine (and all R code beyond this point is a reflection of their choice), but it is worth noting that combinatorists–the people who have the most claim to that dumb list of numbers, generally agree that it should be indexed as .

R Code:

ElapsedTime<-system.time({
##########################
### Function to get a vector of all Fibonacci #'s below n
FibsBelow <- function(n){
  fib <- c(1, 1)
  i <- 2
  while (sum(tail(fib, 2)) < n){
    i <- i+1
    fib[i] <- fib[i-1]+fib[i-2]
  }
  fib
}
###
n <- 4*10^6
 
fib.numbers<-FibsBelow(n)
answer<-sum(fib.numbers[-which(fib.numbers%%2 == 0)])
##########################
})[3]
ElapsedMins<-floor(ElapsedTime/60)
ElapsedSecs<-(ElapsedTime-ElapsedMins*60)
cat(sprintf("\nThe answer is:  %d\nTotal elapsed time:  %d minutes and %f seconds\n",answer, ElapsedMins, ElapsedSecs))

Output: The answer is: 4613732 Total elapsed time: 0 minutes and 0.001000 seconds