diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index 57ee2c2..46aa10f 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -32,7 +32,7 @@ namespace ProjectEulerCS{ private static readonly List _PROBLEM_NUMBERS = new List() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 67}; + 20, 21, 22, 23, 24, 25, 26, 67}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -66,6 +66,7 @@ namespace ProjectEulerCS{ case 23: problem = new Problem23(); break; case 24: problem = new Problem24(); break; case 25: problem = new Problem25(); break; + case 26: problem = new Problem26(); break; case 67: problem = new Problem67(); break; } return problem; diff --git a/ProjectEulerCS/Problems/Problem26.cs b/ProjectEulerCS/Problems/Problem26.cs new file mode 100644 index 0000000..d9e84b7 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem26.cs @@ -0,0 +1,133 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem26.cs +//Matthew Ellison +// Created: 09-11-20 +//Modified: 09-11-20 +//What is the index of the first term in the Fibonacci sequence to contain 1000 digits? +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses +/* + 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 . +*/ + + +using System.Collections.Generic; + + +namespace ProjectEulerCS.Problems{ + public class Problem26 : Problem{ + //Variables + //Static variables + private const int TOP_NUM = 999; //The number of digits needed in the number + //Instance variables + private int longestCycle; //The length of the longest cycle + private int longestNumber; //The starting denominator of the longest cycle + public int LongestCycle{ + get{ + if(!solved){ + throw new Unsolved(); + } + return longestCycle; + } + } + public int LongestNumber{ + get{ + if(!solved){ + throw new Unsolved(); + } + return longestNumber; + } + } + public override string Result{ + get{ + if(!solved){ + throw new Unsolved(); + } + return $"The longest cycle is {longestCycle} digits long\nIt started with the number {longestNumber}"; + } + } + + //Functions + //Constructor + public Problem26() : base($"Find the value of d <= {TOP_NUM} for which 1/d contains the longest recurring cycle in its decimal fraction part."){ + longestCycle = 0; + longestNumber = 0; + } + //Operational functions + //Solve the problem + public override void Solve(){ + //If the problem has already been solved do nothing and end the function + if(solved){ + return; + } + + //Start the timer + timer.Start(); + + //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(int denominator = 2;denominator <= TOP_NUM;++denominator){ + List denomList = new List(); + bool endFound = false; //A flag for when we have found an end to the number (either a cycle or a 0 for remainder) + bool cycleFound = false; //A flag to indicate a cycle was detected + int numerator = 1; //The numerator that will be divided. Always starts at 1 + while(!endFound){ + //Get the remainder after the division + int remainder = numerator % denominator; + //Check if the remainder is 0 and set the flag + if(remainder == 0){ + endFound = true; + } + //Check if the remainder is in the list and set the appropriate flags + else if(denomList.Contains(remainder)){ + endFound = true; + cycleFound = true; + } + //Else add it to the list + else{ + denomList.Add(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(denomList.Count > longestCycle){ + longestCycle = denomList.Count; + longestNumber = denominator; + } + } + } + + //Stop the timer + timer.Stop(); + + //Throw a flag to show the problem is solved + solved = true; + } + //Reset the problem so it can be run again + public override void Reset(){ + base.Reset(); + longestCycle = 0; + longestNumber = 1; + } + } +} + +/* Results: +The longest cycle is 982 digits long +It started with the number 983 +It took an average of 4.657 milliseconds to run this problem through 100 iterations +*/