Files
ProjectEulerPython/Problem1.py

59 lines
2.1 KiB
Python

#ProjectEuler/Python/Problem1.py
#Matthew Ellison
# Created: 01-26-19
#Modified: 03-28-19
#What is the sum of all the multiples of 3 or 5 that are less than 1000
#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
def Problem1():
#Setup your variables
fullSum = 0 #Holds the sum of all the correct numbers
#Start at 3 and start counting up, checking if any of the numbers are divisible by 3 or 5
#This method skips the problem of numbers that are divisible by both, like 15, being added twice
num = 3
while(num < 1000):
if((num % 3) == 0): #3 Will be triggered more often, putting it first makes the algorithm more efficient
fullSum += num
elif((num % 5) == 0):
fullSum += num
num += 1
#Print the results
print("The sum of all the multiples of 3 or 5 is " + str(fullSum))
#If you are running this file, automatically start the correct function
if(__name__ == "__main__"):
timer = Stopwatch() #Used to determine the algorithm's run time
timer.start() #Start the timer
Problem1() #Call the function that answers the problem
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 the multiples of 3 or 5 is 233168
It took 114.142 microseconds to run this algorithm
"""