mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Updated to use new library layout
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
#Project Euler/Python/Problem9.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-29-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
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,14 +23,13 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import math
|
||||
|
||||
|
||||
class Problem9(Problem):
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.")
|
||||
self.a = 1
|
||||
self.b = 0
|
||||
@@ -39,7 +38,7 @@ class Problem9(Problem):
|
||||
|
||||
#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
|
||||
@@ -47,6 +46,7 @@ class Problem9(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start with the lowest possible a , 1, and search for the b and c to complete the triplet
|
||||
while((self.a <= (1000 / 3)) and (not self.found)):
|
||||
#Setup b and c
|
||||
@@ -66,6 +66,7 @@ class Problem9(Problem):
|
||||
else:
|
||||
self.a += 1
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -73,7 +74,7 @@ class Problem9(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.a = 1
|
||||
self.b = 0
|
||||
@@ -82,35 +83,25 @@ class Problem9(Problem):
|
||||
|
||||
#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 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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the length of the first side")
|
||||
self.solvedCheck("first side")
|
||||
return self.a
|
||||
#Returns the length of the second side
|
||||
def getSideB(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the length of the second side")
|
||||
self.solvedCheck("second side")
|
||||
return self.b
|
||||
#Returns the length of the hyp
|
||||
def getSideC(self) -> float:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the length of the hyp")
|
||||
self.solvedCheck("third side")
|
||||
return self.c
|
||||
#Returns the product of the 3 sides
|
||||
def getProduct(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the length first side")
|
||||
self.solvedCheck("product of all three sides")
|
||||
return int(self.a * self.b * self.c)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user