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 @@
|
||||
#ProjectEuler/Python/Problem14.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-31-19
|
||||
#Modified: 07-18-20
|
||||
#Modified: 10-30-20
|
||||
"""
|
||||
The following iterative sequence is defined for the set of positive integers:
|
||||
n → n/2 (n is even)
|
||||
@@ -28,7 +28,6 @@ Which starting number, under one million, produces the longest chain?
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Stopwatch import Stopwatch
|
||||
from Unsolved import Unsolved
|
||||
|
||||
|
||||
@@ -64,11 +63,9 @@ class Problem14(Problem):
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the results
|
||||
self.result = "The number " + str(self.maxNum) + " produced a chain of " + str(self.maxLength) + " steps"
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
|
||||
#This function follows the rules of the sequence and returns its length
|
||||
def checkSeries(self, num: int) -> int:
|
||||
length = 1 #Start at 1 because you need to count the starting number
|
||||
@@ -82,6 +79,7 @@ class Problem14(Problem):
|
||||
length += 1
|
||||
|
||||
return length
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
super().reset()
|
||||
@@ -89,6 +87,12 @@ class Problem14(Problem):
|
||||
self.maxNum = 0
|
||||
|
||||
#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 number {self.maxNum} produced a chain of {self.maxLength} steps"
|
||||
#Returns the length of the requested chain
|
||||
def getLength(self):
|
||||
#If the problem hasn't been solved throw an exception
|
||||
@@ -102,14 +106,6 @@ class Problem14(Problem):
|
||||
raise Unsolved("You must solve the problem before you can get the number that started the series")
|
||||
return self.maxNum
|
||||
|
||||
#If you are running this file, automatically start the correct function
|
||||
if __name__ == "__main__":
|
||||
problem = Problem14()
|
||||
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 number 837799 produced a chain of 525 steps
|
||||
|
||||
Reference in New Issue
Block a user