Files
ProjectEulerPython/Problem16.py

61 lines
2.2 KiB
Python

#Project Euler/Python/Problem16.py
#Matthew Ellison
# Created: 02-03-19
#Modified: 03-28-19
#What is the sum of the digits of the number 2^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 Problem16():
#Setup the variables
sumOfNum = 0 #Holds the sum of the numbers
#Get the number
num = 2 ** 1000
#Change the number to a string
stringOfNum = str(num)
#Step through the string one element at a time
for cnt in range(0, len(stringOfNum)):
#Change the character to an int and add it to the sum
sumOfNum += int(stringOfNum[cnt])
#Print the result
print("2^1000 = " + stringOfNum)
print("The sum of the digits is: " + str(sumOfNum))
#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
Problem16() #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:
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
The sum of the digits is: 1366
It took 86.206 microseconds to run this algorithm
"""