mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
#ProjectEuler/Python/Problem2.py
|
|
#Matthew Ellison
|
|
# Created: 01-26-19
|
|
#Modified: 03-28-19
|
|
#The sum of the even Fibonacci numbers less than 4,000,000
|
|
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
|
|
"""
|
|
Copyright (C) 2019 Matthew Ellison
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
|
|
from Stopwatch import Stopwatch
|
|
from Algorithms import getAllFib
|
|
|
|
__topNumber = 4000000
|
|
|
|
|
|
def Problem2():
|
|
#Setup your variables
|
|
fibSum = 0 #Holds the sum of the Fibonacci numbers
|
|
fibNums = [] #An array that holds the Fibonacci numbers
|
|
|
|
#Get all of the fibonacci numbers
|
|
fibNums = getAllFib(__topNumber - 1) #Send it - 1 because it is < __topNumber
|
|
|
|
#Determine if each number is odd or even
|
|
for num in fibNums:
|
|
#If it is even add it to the running sum
|
|
if((num % 2) == 0):
|
|
fibSum += num
|
|
|
|
#Print the results
|
|
print("The sum of all even Fibonacci numbers less than " + str(__topNumber) + " is " + str(fibSum))
|
|
|
|
|
|
#If you are running this file, automatically start the correct function
|
|
if __name__ == '__main__':
|
|
timer = Stopwatch() #Use to determine the algorithm's run time
|
|
timer.start() #Start the timer
|
|
Problem2() #Call the function that answers the question
|
|
timer.stop() #Stop the timer
|
|
#Print the results of the timer
|
|
print("It took " + timer.getString() + " to run this algorithm")
|
|
|
|
"""Results:
|
|
The sum of all even Fibonacci numbers less than 4000000 is 4613732
|
|
It took 27.621 microseconds to run this algorithm
|
|
"""
|