Summing Fibonacci numbers
In this recipe, we will sum the even-valued terms in the Fibonacci sequence whose values do not exceed four million. The Fibonacci series is a sequence of integers starting with zero, where each number is the sum of the previous two; except, of course, the first two numbers zero and one.
Note
For more information, read the Wikipedia article about Fibonacci numbers at http://en.wikipedia.org/wiki/Fibonacci_number .
This recipe uses a formula based on the golden ratio, which is an irrational number with special properties comparable to pi. It we will use the
sqrt
,
log
,
arange
,
astype
, and
sum
functions.
How to do it...
The first thing to do is calculate the golden ratio (http://en.wikipedia.org/wiki/Golden_ratio), also called the golden section or golden mean.
Calculate the golden ratio.
We will be using the
sqrt
function to calculate the square root of five:phi = (1 + numpy.sqrt(5))/2 print "Phi", phi
This prints the golden mean:
Phi 1.61803398875
Find the index below...