HOW TO FIND THE SUM OF EVEN FIBONACCI NUMBERS USING PYTHON (#2 PROJECT EULER)
Today, we are considering the second problem in Project Euler using Python where we find the sum of even Fibonacci numbers whose values do not exceed 4 million. First of all, let’s define a problem below:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.https://projecteuler.net/problem=2
Ok, let’s dive into the right analysis.
Who is Fibonacci?
Leonardo Fibonacci was the most talented Italian mathematician who lived around 1170-1250. He introduced to Europe the sequence of Fibonacci numbers, which he used as an example in Liber Abaci.
For more, you can read here:
How to build fibonacci number in python using recursion?
Fibonacci numbers have a very interesting feature: each number is the sum of the previous two numbers if we start from 0 and 1, we will see the next numbers are: 2, 3, 5 and so on. That is so amazing property.
Before I will create my function with Fibonacci numbers, I set an empty list, which will be the place where I will be collecting my new numbers created by my function.
Ok, let’s define our function by fib and use some sort of recursion appending a new number to our list:
def fib(n): '''Generate Fibonacci numbers''' for i in range(n): if i==0: fibonacci.append(1) fibonacci=1 elif i==1: fibonacci.append(2) fibonacci=2 else: fibonacci.append(fibonacci[i-1]+fibonacci[i-2]) fibonacci[i]=fibonacci[i-1]+fibonacci[i-2]
How to find the sum of even Fibonacci numbers in Python whose values not exceed 4 million?
And yes, we have all Fibonacci numbers, is the end? Of course not! We need to come back to our main task. We need to find the sum of even Fibonacci numbers whose values do not exceed four million. And again to find those values, first, let’s find even numbers from the Fibonacci list, but before we append our even value to the new list let’s check if not exceed 4 million. If we will find all those values we can easily sum those values using the build-in sum function and print answers for our problem.
even_fib_not_exceed_four_million= for i in fibonacci: if i%2 == 0: if i < 4*(10**6): even_fib_not_exceed_four_million.append(i) k=i else: k=i print('Value '+str(k)+' exceed 4 milion in fibonacci sequence') break if k < 4*(10**6): print('Even fibonacci numbers:') print(even_fib_not_exceed_four_million) print('Sum of even numbers is '+ str(sum(even_fib_not_exceed_four_million)))
If we try to call function fib(10). It prints the sum of even numbers:
But if we call fib(100), our function finds those values that exceed 4 million:
My all code in Python you can find below:
Also, if you are interested, you can find my previous solved problem, and all explanations here: https://cyberula.com/amazing-coding-challenge-in-python-to-try-today-2021/
In the end, I will encourage you to find your own solution to this problem in your known programming language and watch a video on YT, and see how beautiful and amazing applications can have maths in nature:
If you will see any bugs or typos here, don’t hesitate to comment below. Happy coding!