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:
@@ -23,9 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem37(Problem):
|
||||
@@ -34,14 +32,14 @@ class Problem37(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted).")
|
||||
self.truncPrimes = [] #All numbers that are truncatable primes
|
||||
self.sumOfTruncPrimes = 0 #The sum of all elements in truncPrimes
|
||||
|
||||
#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
|
||||
@@ -49,8 +47,9 @@ class Problem37(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Create the sieve and get the first prime number
|
||||
sieve = Algorithms.primeGenerator()
|
||||
sieve = NumberAlgorithms.primeGenerator()
|
||||
currentPrime = next(sieve)
|
||||
#Loop through the sieve until you get to __last_prime_before_check
|
||||
while(currentPrime < self.__last_prime_before_check):
|
||||
@@ -77,7 +76,7 @@ class Problem37(Problem):
|
||||
primeSubstring = primeString[truncLoc::]
|
||||
#Convert the string to an int and see if the number is still prime
|
||||
newPrime = int(primeSubstring)
|
||||
if(not Algorithms.isPrime(newPrime)):
|
||||
if(not NumberAlgorithms.isPrime(newPrime)):
|
||||
isTruncPrime = False
|
||||
break
|
||||
#Start removing digits from the right and see if the number stays prime
|
||||
@@ -87,7 +86,7 @@ class Problem37(Problem):
|
||||
primeSubstring = primeString[0:len(primeString) - truncLoc]
|
||||
#Convert the string to an int and see if the number is still prime
|
||||
newPrime = int(primeSubstring)
|
||||
if(not Algorithms.isPrime(newPrime)):
|
||||
if(not NumberAlgorithms.isPrime(newPrime)):
|
||||
isTruncPrime = False
|
||||
break
|
||||
#If the number remained prime through all operations add it to the vector
|
||||
@@ -99,6 +98,7 @@ class Problem37(Problem):
|
||||
#Get the sum of all elements in the truncPrimes vector
|
||||
self.sumOfTruncPrimes = sum(self.truncPrimes)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -106,7 +106,7 @@ class Problem37(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.truncPrimes = []
|
||||
self.sumOfTruncPrimes = 0
|
||||
@@ -114,23 +114,18 @@ class Problem37(Problem):
|
||||
#Gets
|
||||
#Returns a string with the solution to the problem
|
||||
def getResult(self) -> str:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the result")
|
||||
self.solvedCheck("result")
|
||||
return f"The sum of all left and right truncatable primes is {self.sumOfTruncPrimes}"
|
||||
#Returns the list of primes that can be truncated
|
||||
def getTruncatablePrimes(self) -> list:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the truncatable primes")
|
||||
self.solvedCheck("list of truncatable primes")
|
||||
return self.truncPrimes
|
||||
#Returns the sum of all elements in truncPrimes
|
||||
def getSumOfPrimes(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the sum of the truncatable primes")
|
||||
self.solvedCheck("sum of truncatable primes")
|
||||
return self.sumOfTruncPrimes
|
||||
|
||||
|
||||
""" Results:
|
||||
The sum of all left and right truncatable primes is 748317
|
||||
It took an average of 224.657 milliseconds to run this problem through 100 iterations
|
||||
|
||||
Reference in New Issue
Block a user