Project Euler Problems 1 and 2
Multiples of 3 or 5 and even Fibonacci numbers in Python
In order to stay fresh with general programming skills I am going to attempt various Project Euler problems and walk through my solutions. For those of you that do not know Project Euler it "is a series of challenging mathematical/computer programming problems." Simply put, it is a great way to practice your computer programming skills.
In this blog I'm going to work on Project Euler problems 1 and 2.
Breaking down the problem:
It's pretty straightforward. We want all the natural numbers (so positive integers... no decimals) under 1,000 that are multiples of 3 or 5. A multiple is simply "a number that can be divided by another number without a remainder." So, multiples of 3 include 3, 6, 9, 12...
Then we just want to take the sum of all those numbers.
I am going to create a function with 2 arguments:
- A list of the numbers we want multiples of (so in this case 3 and 5)
- The max number we want (in this case 1000)
The function will then do the following steps:
- Initialize a list (
sum_list). This is where we will store the multiples of the numbers we are interested in. For the case of this problem, I mean all the multiples of 3 and 5. - Loop through 1 and the
max_number(1000 for this particular problem). We'll call this numberi. - Loop through each of the dividers (e.g. 3 and 5) and use the modulo operator,
%, to determine if the number,i, is even or odd. The modulo operator returns the remainder of a division problem (e.g.4 % 1 = 1). - If the number
ihas no remainder when divided by a divider we will.append(or add) it to thesum_listwe created earlier. - One problem this creates is there could be duplicates. For example, 15 would show up twice in
sum_listas both 3 and 5 go into it. We can solve this by removing duplicates in thesum_list. Theset()function is any easy way to do this. The function converts the object into a Python set (one of the 4 major Python data classes along with lists, tuples, and dictionaries). Sets "do not allow duplicate values" so the use oflist(set(sum_list))will convert the sum_list into a set, effectively dropping duplicate values, then converting it back into a list. - The last step is to use the
sum()function to calculate the sum of all the multiples stored insum_list.
def euler_1(dividers, max_number):
sum_list = []
for i in range(1, max_number):
for div in dividers:
if i % div == 0:
sum_list.append(i)
sum_list = list(set(sum_list))
return(sum(sum_list))
Running our function with the arguments from the question (multiples of 3 and 5 up to 1000) we get an answer of 233,168.
euler_1([3,5], 1000)
Problem 1: Even Fibonacci numbers
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.
Breaking down the problem:
This problem will involve 3 parts:
- Creating a Fibonacci sequence that stops right before 4,000,000
- Sorting out only the even numbers
- Finding the sum of all those even numbers
Similar to the first Euler problem, I'm going to create a function that takes two arguments:
- The maximum number for the Fibonacci sequence (4,000,000) in this case
- And, a boolean variable determining if we want to sort even numbers
The function will then do the following steps:
- Create a variable called
modulo_divthat will allow us to toggle whether we want to sum the even-valued terms (such as in this problem) or odd-valued terms - Create a list,
fibonacci, with the first two terms of the Fibonacci sequence (1 and 2) - Create a variable,
i, which will serve as an iterator - Put
iinto a while loop. Then, add together the last two numbers of thefibonaccilist and make that sum the value ofi. For example, on the first iteration we will sum 1 and 2 makingiequal to 3. - As long as the value of
iis less than 4,000,000 add it to the end offibonacciusingappend(). If the value ofiexceeds 4,000,000 do not add it to the list and break the loop. - Once the loop is broken we create a new list,
fibonacci_portion. Use a Python list comprehension to go through thefibonaccilist and only add even numbers. It will be the same method we used to gather odd numbers in the previous problem (using the modulo operator). - Finally, return the sum of the fibonacci_portion list (so all even numbers in the Fibonacci sequence up to 4,000,000)
def euler_2(max_number, even = True):
if even == True:
modulo_div = 2
else:
modulo_div = 1
fibonacci = [1,2]
i = 1
while i > 0:
i = sum(fibonacci[-2:])
if i > 4000000:
break
fibonacci.append(i)
fibonacci_portion = [j for j in fibonacci if j % modulo_div == 0]
return(sum(fibonacci_portion))
Running our function with the arguments from the question (a maximum of 4,000,000 and looking at even numbers) we get an answer of 4,613,732.
euler_2(4000000, even = True)