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:
56
Problem6.py
Normal file
56
Problem6.py
Normal file
@@ -0,0 +1,56 @@
|
||||
#ProjectEuler/Python/Problem6.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-28-19
|
||||
#Modified: 03-28-19
|
||||
#Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum
|
||||
#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 #To time the algorithm
|
||||
|
||||
|
||||
def Problem6():
|
||||
#Setup the variables
|
||||
sumOfSquares = 0 #Holds the sum of the square of the numbers
|
||||
squareOfSum = 0 #Holds the square of the sum of the numbers
|
||||
|
||||
#Run through all numbers from 1-100 and add them to the approriate sums
|
||||
for num in range(1, 101):
|
||||
sumOfSquares += num * num #Get the sum of the squares of the first 100 natural numbers
|
||||
squareOfSum += num #Get the sum of the first 100 natural numbers so you can square it later
|
||||
|
||||
#Square the normal sum
|
||||
squareOfSum *= squareOfSum
|
||||
|
||||
#Print the result
|
||||
print("The difference between the sum of the squares and the square of the sum of the numbers 1-100 is " + str(abs(sumOfSquares - squareOfSum)))
|
||||
|
||||
#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
|
||||
Problem6() #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 difference between the sum of the squares and the square of the sum of the numbers 1-100 is 25164150
|
||||
It took 24.384 microseconds to run this algorithm
|
||||
"""
|
||||
Reference in New Issue
Block a user