mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Updated all problems to use fstrings and results()
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem6.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-28-19
|
||||
#Modified: 07-18-20
|
||||
#Modified: 10-29-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
|
||||
"""
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Stopwatch import Stopwatch
|
||||
from Unsolved import Unsolved
|
||||
|
||||
|
||||
@@ -50,7 +49,7 @@ class Problem6(Problem):
|
||||
self.timer.start()
|
||||
|
||||
#Run through all numbers from 1-100 and add them to the approriate sums
|
||||
for num in range(1, 101):
|
||||
for num in range(self.__startNum, self.__endNum + 1):
|
||||
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
|
||||
@@ -59,9 +58,6 @@ class Problem6(Problem):
|
||||
#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
|
||||
|
||||
@@ -72,6 +68,12 @@ class Problem6(Problem):
|
||||
self.sumOfSquares = 0
|
||||
|
||||
#Gets
|
||||
#Returns the result of solving the problem
|
||||
def getResult(self):
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can see the result")
|
||||
return f"The difference between the sum of the squares and the square of the sum of the numbers 1-100 is {abs(self.sumOfSquares - self.squareOfSum)}"
|
||||
#Returns the sum of all the squares
|
||||
def getSumOfSquares(self) -> int:
|
||||
#If the problem hasn't been solved throw an exceptions
|
||||
@@ -91,14 +93,6 @@ class Problem6(Problem):
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user