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 @@
|
||||
#Project Euler/Python/Problem9.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-29-19
|
||||
#Modified: 07-18-20
|
||||
#Modified: 10-30-20
|
||||
#There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
|
||||
#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
|
||||
import math
|
||||
|
||||
@@ -70,14 +69,9 @@ class Problem9(Problem):
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the results
|
||||
if(self.found):
|
||||
self.result = "The Pythagorean triplet where a + b + c = 1000 is " + str(self.a) + " " + str(self.b) + " " + str(int(self.c)) + "\nThe product of those numbers is " + str(int(self.a * self.b * self.c))
|
||||
else:
|
||||
self.result = "Could not find the triplet where a + b + c = 1000"
|
||||
|
||||
#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()
|
||||
@@ -87,6 +81,13 @@ class Problem9(Problem):
|
||||
self.found = False
|
||||
|
||||
#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 Pythagorean triplet where a + b + c = 1000 is {self.a} {self.b} {int(self.c)}\n" \
|
||||
f"The product of those numbers is {int(self.a * self.b * self.c)}"
|
||||
#Returns the length of the first side
|
||||
def getSideA(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
@@ -112,14 +113,6 @@ class Problem9(Problem):
|
||||
raise Unsolved("You must solve the problem before you can get the length first side")
|
||||
return int(self.a * self.b * self.c)
|
||||
|
||||
#If you are running this file, automatically start the correct function
|
||||
if __name__ == "__main__":
|
||||
problem = Problem9()
|
||||
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 Pythagorean triplet where a + b + c = 1000 is 200 375 425
|
||||
|
||||
Reference in New Issue
Block a user