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,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem27.py
|
||||
#Matthew Ellison
|
||||
# Created: 09-15-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
|
||||
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
|
||||
"""
|
||||
@@ -23,14 +23,13 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem27(Problem):
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0")
|
||||
self.topA = 0 #The A for the most n's generated
|
||||
self.topB = 0 #The B for the most n's generated
|
||||
@@ -39,7 +38,7 @@ class Problem27(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,8 +46,9 @@ class Problem27(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get the primes
|
||||
primes = Algorithms.getPrimes(12000) #A list of all primes that could possibly be generated with this formula
|
||||
primes = NumberAlgorithms.getPrimes(12000) #A list of all primes that could possibly be generated with this formula
|
||||
#Start with the lowest possible A and check all possibilities after that
|
||||
for a in range(-999, 999):
|
||||
#Start with the lowest possible B and check all possibilities after that
|
||||
@@ -67,6 +67,7 @@ class Problem27(Problem):
|
||||
self.topB = b
|
||||
self.topA = a
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -79,7 +80,7 @@ class Problem27(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.topA = 0
|
||||
self.topB = 0
|
||||
@@ -88,34 +89,31 @@ class Problem27(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 greatest number of primes found is {self.topN}\n" \
|
||||
f"It was found with A = {self.topA}, B = {self.topB}\n" \
|
||||
f"The product of A and B is {self.topA * self.topB}"
|
||||
#Returns the top A that was generated
|
||||
def getTopA(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the top A")
|
||||
self.solvedCheck("largest A")
|
||||
return self.topA
|
||||
|
||||
#Returns the top B that was generated
|
||||
def getTopB(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the top B")
|
||||
self.solvedCheck("largest B")
|
||||
return self.topA
|
||||
|
||||
#Returns the top N that was generated
|
||||
def getTopN(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the top N")
|
||||
self.solvedCheck("largest N")
|
||||
return self.topA
|
||||
|
||||
#Returns the product of A and B for the answer
|
||||
def getProduct(self) -> int:
|
||||
self.solvedCheck("product of A and B")
|
||||
return self.topA * self.topB
|
||||
|
||||
|
||||
""" Results:
|
||||
The greatest number of primes found is 70
|
||||
|
||||
Reference in New Issue
Block a user