From 6b8c6e026665fee67cf24d22cfd234559b56323e Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 24 Aug 2020 21:15:10 -0400 Subject: [PATCH] Added solution to problem 14 --- ProjectEulerCS/ProblemSelection.cs | 3 +- ProjectEulerCS/Problems/Problem13.cs | 2 +- ProjectEulerCS/Problems/Problem14.cs | 121 +++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 ProjectEulerCS/Problems/Problem14.cs diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index e1c2f93..f0af517 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -31,7 +31,7 @@ namespace ProjectEulerCS{ //Holds the valid problem numbers private static readonly List _PROBLEM_NUMBERS = new List() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13}; + 10, 11, 12, 13, 14}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -53,6 +53,7 @@ namespace ProjectEulerCS{ case 11: problem = new Problem11(); break; case 12: problem = new Problem12(); break; case 13: problem = new Problem13(); break; + case 14: problem = new Problem14(); break; } return problem; } diff --git a/ProjectEulerCS/Problems/Problem13.cs b/ProjectEulerCS/Problems/Problem13.cs index ed9552b..a63b784 100644 --- a/ProjectEulerCS/Problems/Problem13.cs +++ b/ProjectEulerCS/Problems/Problem13.cs @@ -1,4 +1,4 @@ -//ProjectEuler/ProjectEulerCS/src/Problems/Problem12.cs +//ProjectEuler/ProjectEulerCS/src/Problems/Problem13.cs //Matthew Ellison // Created: 08-24-20 //Modified: 08-24-20 diff --git a/ProjectEulerCS/Problems/Problem14.cs b/ProjectEulerCS/Problems/Problem14.cs new file mode 100644 index 0000000..06fdf99 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem14.cs @@ -0,0 +1,121 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem14.cs +//Matthew Ellison +// Created: 08-24-20 +//Modified: 08-24-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 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 . +*/ + + +namespace ProjectEulerCS.Problems{ + public class Problem14 : Problem{ + //Variables + //Static variables + private const long MAX_NUM = 1000000 - 1; //This is the top number that you will be checking against the series + //Instance variables + private long maxLength; //This is the length of the longest chain + public long Length{ + get{ + if(!solved){ + throw new Unsolved(); + } + return maxLength; + } + } + private long maxNum; //This is the starting number of the longest chain + public long StartingNumber{ + get{ + if(!solved){ + throw new Unsolved(); + } + return maxNum; + } + } + + //Functions + //Constructor + public Problem14() : base("Which starting number, under one million, produces the longest chain using the itterative sequence?"){ + maxLength = 0; + maxNum = 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(); + + //Loop through all numbers <= MAX_NUM and check them against the series + for(long currentNum = 1;currentNum <= MAX_NUM;++currentNum){ + long currentLength = CheckSeries(currentNum); + //If the current number has a longer series than the max then the current becomes the max + if(currentLength > maxLength){ + maxLength = currentLength; + maxNum = currentNum; + } + } + + //Stop the timer + _timer.Stop(); + + //Throw a flag to show the problem is solved + solved = true; + + //Save the results + _result = "The number " + maxNum + " produced a chain of " + maxLength + " steps"; + } + //This function follows the rules of the sequence and returns its length + private long CheckSeries(long num){ + long length = 1; //Start at 1 because you need to count the starting number + + //Follow the series, adding 1 for each time you take + while(num > 1){ + if((num % 2) == 0){ + num /= 2; + } + else{ + num = (3 * num) + 1; + } + ++length; + } + + //Return the length of the series + return length; + } + //Reset the problem so it can be run again + public override void Reset(){ + base.Reset(); + maxLength = 0; + maxNum = 0; + } + } +} + +/* Results: +The number 837799 produced a chain of 525 steps +It took an average of 249.883 milliseconds to run this problem through 100 iterations +*/