mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Initial commit with existing files
This commit is contained in:
61
Problem20.py
Normal file
61
Problem20.py
Normal file
@@ -0,0 +1,61 @@
|
||||
#ProjectEuler/Python/Problem20.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-14-19
|
||||
#Modified: 03-28-19
|
||||
#What is the sum of the digits of 100!
|
||||
#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
|
||||
|
||||
__TOP_NUM = 100 #The number that you are trying to find the factorial of
|
||||
|
||||
|
||||
def Problem20():
|
||||
num = 1 #Holds the number being calculated
|
||||
sumOfNum = 0 #Holds the sum of the digits of num
|
||||
|
||||
#Run through every number from 1 to 100 and multiply it by the current num to get 100!
|
||||
for cnt in range(1, __TOP_NUM + 1):
|
||||
num *= cnt
|
||||
|
||||
#Get a string of the number because it is easier to pull appart the individual charaters for the sum
|
||||
numString = str(num)
|
||||
#Run through every character in the string, convert it back to an integer and add it to the running sum
|
||||
for char in numString:
|
||||
sumOfNum += int(char)
|
||||
|
||||
#Print the results
|
||||
print("100! = " + numString)
|
||||
print("The sum of the digits is: " + str(sumOfNum))
|
||||
|
||||
|
||||
#This starts the correct function if called directly
|
||||
if __name__ == "__main__":
|
||||
timer = Stopwatch()
|
||||
timer.start()
|
||||
Problem20()
|
||||
timer.stop()
|
||||
print("It took " + timer.getString() + " to run this algorithm")
|
||||
|
||||
""" Results:
|
||||
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
||||
The sum of the digits is: 648
|
||||
It took 99.670 microseconds to run this algorithm
|
||||
"""
|
||||
Reference in New Issue
Block a user