Updated to use new library layout

This commit is contained in:
2021-07-24 16:13:05 -04:00
parent d18b3fa9f6
commit 84555edd31
39 changed files with 515 additions and 709 deletions

View File

@@ -1,11 +1,11 @@
#ProjectEuler/Python/Problem6.py
#Matthew Ellison
# Created: 01-28-19
#Modified: 10-29-20
#Modified: 07-24-21
#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
Copyright (C) 2021 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
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Unsolved import Unsolved
class Problem6(Problem):
@@ -33,14 +32,14 @@ class Problem6(Problem):
#Functions
#Constructor
def __init__(self):
def __init__(self) -> None:
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):
def solve(self) -> None:
#If the problem has already been solved do nothing and end the function
if(self.solved):
return
@@ -48,6 +47,7 @@ class Problem6(Problem):
#Start the timer
self.timer.start()
#Run through all numbers from 1-100 and add them to the approriate sums
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
@@ -55,6 +55,7 @@ class Problem6(Problem):
#Square the normal sum
self.squareOfSum *= self.squareOfSum
#Stop the timer
self.timer.stop()
@@ -62,35 +63,27 @@ class Problem6(Problem):
self.solved = True
#Reset the problem so it can be run again
def reset(self):
def reset(self) -> None:
super().reset()
self.squareOfSum = 0
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")
def getResult(self) -> str:
self.solvedCheck("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
if(not self.solved):
raise Unsolved("You must solve the problem before you can get the sum of squares")
self.solvedCheck("sum of the 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")
self.solvedCheck("square of the sums")
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")
self.solvedCheck("difference between the two numbers")
return abs(self.sumOfSquares - self.squareOfSum)