mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
118 lines
3.8 KiB
Python
118 lines
3.8 KiB
Python
#ProjectEuler/Python/Problem14.py
|
|
#Matthew Ellison
|
|
# Created: 01-31-19
|
|
#Modified: 07-18-20
|
|
"""
|
|
The following iterative sequence is defined for the set of positive integers:
|
|
n → n/2 (n is even)
|
|
n → 3n + 1 (n is odd)
|
|
Which starting number, under one million, produces the longest chain?
|
|
"""
|
|
#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
|
|
|
|
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
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
|
|
from Problems.Problem import Problem
|
|
from Stopwatch import Stopwatch
|
|
from Unsolved import Unsolved
|
|
|
|
|
|
class Problem14(Problem):
|
|
#Variables
|
|
__topNum = 1000000 - 1 #The largest number that you will check against the chain
|
|
|
|
#Functions
|
|
#Constructor
|
|
def __init__(self):
|
|
super().__init__("Which starting number, under one million, produces the longest chain using the itterative sequence?")
|
|
self.maxLength = 0
|
|
self.maxNum = 0
|
|
|
|
#Operational functions
|
|
#Solve the problem
|
|
def solve(self):
|
|
#If the problem has already been solved do nothing and end the function
|
|
if(self.solved):
|
|
return
|
|
|
|
#Start the timer
|
|
self.timer.start()
|
|
|
|
#Loop through all number <= topNum and check them against the series
|
|
for currentNum in range(1, self.__topNum + 1):
|
|
currentLength = self.checkSeries(currentNum)
|
|
#If the current number has a longer series than the max then the current becomes the max
|
|
if(currentLength > self.maxLength):
|
|
self.maxLength = currentLength
|
|
self.maxNum = currentNum
|
|
|
|
#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
|
|
|
|
#Follow the series, adding 1 for each step you take
|
|
while(num > 1):
|
|
if((num % 2) == 0):
|
|
num = num / 2
|
|
else:
|
|
num = (3 * num) + 1
|
|
length += 1
|
|
|
|
return length
|
|
#Reset the problem so it can be run again
|
|
def reset(self):
|
|
super().reset()
|
|
self.maxLength = 0
|
|
self.maxNum = 0
|
|
|
|
#Gets
|
|
#Returns the length of the requested chain
|
|
def getLength(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 get the length of the requested chain")
|
|
return self.maxLength
|
|
#Returns the starting number of the requested chain
|
|
def getStartingNumber(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 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 longest chain with a starting number < 1000000 is 837799 with a length of 525
|
|
It took 28.893 seconds to run this algorithm
|
|
"""
|