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,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem25(Problem):
|
||||
@@ -33,14 +32,14 @@ class Problem25(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?")
|
||||
self.number = 0 #The current Fibonacci number
|
||||
self.index = 2 #The index of the current Fibonacci number just calculated
|
||||
|
||||
#Operational functions
|
||||
#Sovle 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,10 +47,12 @@ class Problem25(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Move through all Fibonacci numbers until you reach the one with at least __numDigits digits
|
||||
while(len(str(self.number)) < self.__numDigits):
|
||||
self.index += 1 #Increase the index number. Doing this at the beginning keeps the index correct at the end of the loop
|
||||
self.number = Algorithms.getFib(self.index) #Calculate the number
|
||||
self.number = NumberAlgorithms.getFib(self.index) #Calculate the number
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
@@ -60,30 +61,24 @@ class Problem25(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.number = 0
|
||||
self.index = 2
|
||||
|
||||
#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 first Fibonacci number with {self.__numDigits} digits is {self.number}\n" \
|
||||
f"Its index is {self.index}"
|
||||
#Returns the Fibonacci number asked for
|
||||
def getNumber(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 Fibonacci number")
|
||||
self.solvedCheck("fibonacci number")
|
||||
return self.number
|
||||
#Returns the index of the requested Fibonacci number
|
||||
def getIndex(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 index of the Fibonacci number")
|
||||
self.solvedCheck("index of the fibonacci number")
|
||||
return self.index
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user