mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
#ProjectEulter/Python/Project5.py
|
|
#Matthew Ellison
|
|
# Created: 01-28-19
|
|
#Modified: 03-28-19
|
|
#What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
|
#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
|
|
|
|
__startNum = 1
|
|
__stopNum = 20
|
|
|
|
|
|
def Problem5():
|
|
#Setup the variables
|
|
numFound = False #Holds whether we have found the divisible number yet
|
|
#Start at 20 and loop through all numbers until you find one that works
|
|
#It must be at least 20 to be divisible by 20
|
|
num = 20 #Holds the number that you are currently checking against
|
|
while((not numFound) and (num > 0)): #Set an escape, just in case there is no answer and you overflow
|
|
#Set that you found the number to true, because you set this flag when you don't find it
|
|
numFound = True
|
|
#See if the current number is divisible by all numbers from 1 to 20
|
|
for divisor in range(__startNum, __stopNum + 1):
|
|
#If it is not set a flag to move to the next possible number
|
|
if((num % divisor) != 0):
|
|
numFound = False
|
|
break
|
|
|
|
#Increment the number by 2 to check the next one if you didn't find the number
|
|
if not numFound:
|
|
num += 2
|
|
|
|
#Print the results
|
|
if(num < 0):
|
|
print("There was an error: Could not find a number that fit the criteria")
|
|
else:
|
|
print("The smallest positive number that is evenly divisible by all numbers 1-20 is " + str(num))
|
|
|
|
#If you are running this file, automatically start the correct function
|
|
if __name__ == '__main__':
|
|
timer = Stopwatch() #Determines the algorithm's run time
|
|
timer.start() #Start the timer
|
|
Problem5() #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 smallest positive number that is evenly divisible by all numbers 1-20 is 232792560
|
|
It took 50.236 seconds to run this algorithm
|
|
""" |