mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
107 lines
3.8 KiB
Python
107 lines
3.8 KiB
Python
#ProjectEuler/Python/Problem6.py
|
|
#Matthew Ellison
|
|
# Created: 01-28-19
|
|
#Modified: 07-18-20
|
|
#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) 2020 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 Problems.Problem import Problem
|
|
from Stopwatch import Stopwatch
|
|
from Unsolved import Unsolved
|
|
|
|
|
|
class Problem6(Problem):
|
|
#Variables
|
|
__startNum = 1
|
|
__endNum = 100
|
|
|
|
#Functions
|
|
#Constructor
|
|
def __init__(self):
|
|
super().__init__("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.")
|
|
self.sumOfSquares = 0 #Holds the sum of the squares of all the numbers
|
|
self.squareOfSum = 0 #Holds the square of the sum of all the numbers
|
|
|
|
#Operational functions
|
|
#Solve the problem
|
|
def solve(self):
|
|
#If the problem has already been solved do nothing and end the function
|
|
if(self.solved):
|
|
return
|
|
|
|
#Start the timer
|
|
self.timer.start()
|
|
|
|
#Run through all numbers from 1-100 and add them to the approriate sums
|
|
for num in range(1, 101):
|
|
self.sumOfSquares += (num * num) #Get the sum of the squares of the first 100 natural numbers
|
|
self.squareOfSum += num #Get the sum of the first 100 natural numbers so you can square it later
|
|
#Square the normal sum
|
|
self.squareOfSum *= self.squareOfSum
|
|
|
|
#Stop the timer
|
|
self.timer.stop()
|
|
|
|
#Save the result
|
|
self.result = "The difference between the sum of the squares and the square of the sum of the numbers 1-100 is " + str(abs(self.sumOfSquares - self.squareOfSum))
|
|
|
|
#Throw a flag to show the problem is solved
|
|
self.solved = True
|
|
|
|
#Reset the problem so it can be run again
|
|
def reset(self):
|
|
super().reset()
|
|
self.squareOfSum = 0
|
|
self.sumOfSquares = 0
|
|
|
|
#Gets
|
|
#Returns the sum of all the squares
|
|
def getSumOfSquares(self) -> int:
|
|
#If the problem hasn't been solved throw an exceptions
|
|
if(not self.solved):
|
|
raise Unsolved("You must solve the problem before you can get the sum of squares")
|
|
return self.sumOfSquares
|
|
#Return the sqare of all of the sums
|
|
def getSquareOfSum(self) -> int:
|
|
#If the problem hasn't been solved throw an exceptions
|
|
if(not self.solved):
|
|
raise Unsolved("You must solve the problem before you can get the square of sum")
|
|
return self.squareOfSum
|
|
#Returns the requested difference
|
|
def getDifference(self) -> int:
|
|
#If the problem hasn't been solved throw an exceptions
|
|
if(not self.solved):
|
|
raise Unsolved("You must solve the problem before you can get the difference between the sum of squares and square of sum")
|
|
return abs(self.sumOfSquares - self.squareOfSum)
|
|
|
|
#If you are running this file, automatically start the correct function
|
|
if __name__ == '__main__':
|
|
problem = Problem6()
|
|
print(problem.getDescription()) #Print the description of the problem
|
|
problem.solve() #Solve the problem
|
|
#Print the results
|
|
print(problem.getResult())
|
|
print("It took " + problem.getTime() + " to solve 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
|
|
"""
|