#ProjectEuler/Python/Problem26.py #Matthew Ellison # Created: 07-29-19 #Modified: 08-02-19 #Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. #Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses """ Copyright (C) 2019 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 . """ from Stopwatch import Stopwatch import Algorithms __topNumber = 999 #The largest denominator to be checked def Problem26(): longestCycle = 0 longestNumber = 1 #Start with 1/2 and find out how long the longest cycle is by checking the remainders #Loop through every number from 2-999 and use it for the denominator for denominator in range(2, __topNumber): remainderList = [] #Holds the list of remainders endFound = False #Holds whether we have found an end to the number (either a cycle or a 0 for remainder) cycleFound = False #Holds whether a cycle was detected numerator = 1 #The numerator that will be divided while(not endFound): #Get the remainder after the division remainder = numerator % denominator #Check if the remainder is 0 #If it is set the flag if(remainder == 0): endFound = True #Check if the remainder is in the list #If it is in the list, set the appropriate flags elif remainder in remainderList: endFound = True cycleFound = True #Else add it to the list else: remainderList.append(remainder) #Multiply the remainder by 10 to continue finding the next remainder numerator = remainder * 10 #If a cycle was found check the size of the list against the largest cycle if(cycleFound): #If it is larger than the largest, set it as the new largest if(len(remainderList) > longestCycle): longestCycle = len(remainderList) longestNumber = denominator #Print the results print("The longest cycle is " + str(longestCycle) + " digits long") print("It is started with the number " + str(longestNumber)) #This calls the appropriate functions if the script is called stand along if __name__ == "__main__": timer = Stopwatch() timer.start() Problem26() timer.stop() print("It took " + timer.getString() + " to run this algorithm") """ Results: The longest cycle is 982 digits long It is started with the number 983 It took 182.704 milliseconds to run this algorithm """