Showing posts with label Project Euler Python. Show all posts
Showing posts with label Project Euler Python. Show all posts

Thursday, 1 January 2015

Sum of even Fibonacci numbers below 4 million


Question: We know that fibonacci sequence is 0,1,1,2,3,5,8,13........ This is obtained by taking the first two whole numbers 0, 1 and then we add them we get 1, so the sequence becomes 0,1,1 now consider the two numbers 1,1 and this gives 2 and in this way the sequence is obtained. For further more information you can find about fibonacci sequence here: Fibonacci Sequence.

Let us assume the first 10 Fibonacci numbers(Neglect 0 and 1): 1,2,3,5,8,13,21,34,55,89. Now the even Fibonacci numbers become: 2,8,34. And the sum is 44. In this way we have to find the sum of even fibonacci numbers where the largest number in the group(In the top ten Fibonacci numbers is 34) is less than 4 million. 
Answer: I have commented the code and tried to make it as simple as possible. Please do contact me if you have a solution which is a way different from this so that it can be published here. Also please do comment if you have a doubt. Doubts are dangerous.


#Function to check if number is even.
#Returns True if even and else False
def is_even(n):
        if n%2 == 0:
                return True
        else:
                return False

#Function to generate n fibanocci numbers
def fibanocci(n):
        a = 0
        b = 1
        numbers = []
        numbers.append(a)
        while len(numbers) < n+2:
                numbers.append(b)
                temp = a
                a = b
                b = a+temp
        del numbers[0]
        del numbers[1]
        return numbers

#Function to generate even fibanocci numbers
def even_fibanocci(n):
        lis = fibanocci(n)
        even = []
        for i in lis:
                if is_even(i):
                        even.append(i)
        return even
#Iteration to generate sum of even numbers
j = 1
while True:
        lis = even_fibanocci(j)
        if len(lis) > 0:
                if lis[len(lis)-1] < 4000000:
                        lis = even_fibanocci(j)
                        largest = "Sum(even) of first",j,"fibonacci number is",sum(lis)
                else:
                        break
        j += 1
print largest
Output:

('Sum(even) of first', 34, 'fibonacci number is', 4613732)

So the answer for the question is 4613732. Note: This problem was taken from Project Euler. And the solution is my own.

Monday, 29 December 2014

Multiples of 3 and 5 and their sum


Question: Find the multiples of 3 and 5 and add them till the numbers below 1000. For example if you consider numbers below 10 you will have [3.5,6,9] as the numbers which are divisible by 3 and 5. Their sum will be 23(3+5+6+9), where 23 is the required answer for numbers below 10(<10). Similarly if we consider numbers below 20 you will have [3,6,5,9,10,12,15,18] as the numbers which are divisible by either 3 or 5. Their sum will be 78(3+6+5+9+10+12+15+18) which will be the required answer for numbers below 20.
Similarly find out the sum for the numbers below 1000?

Answer: I have solved this question in two ways, first one I have answered this question using functions and in the second method which doesn't use functions. I have commented the code so that everyone will understand it perfectly. Please do tell me if you don't understand. I will for sure make it clear for you.

Approach using Functions:

#Defining a function as the code may be used any number of times.
#Step1:Defining the name of function
def sum_multiple(n):
#Initialize a variable sum1 = 0
    sum1 = 0
#Use the for loop to loop n-1 times
    for number in range(1,n):
#Make decision if the number is divisible by 3 or 5. Using 'OR' operator
        if number % 3 == 0 or number % 5 == 0:
#If condition is true add the value
            sum1 += number
#Finally print the sum.
    print sum1

#Use the function call for first 9 numbers.
sum_multiple(10) #Expected value of 23

#Use it to give the value for first 999 numbers(numbers below 1000).
sum_multiple(1000) #Value is 233168
Output:

23
233168

Normal Approach:

#Initialize a variable sum1 = 0
sum1 = 0
#Use the for loop to loop 999 times
for number in range(1,1000):
#Make decision if the number is divisible by 3 or 5. Using 'OR' operator
    if number % 3 == 0 or number % 5 == 0:
#If condition is true add the value
        sum1 += number
#Finally print the sum.
print sum1
Output:
233168

Note:This problem was found on Project Euler