From 90957f55dcd82e794ce24bd295e09f6769e74860 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 23 Aug 2020 13:18:24 -0400 Subject: [PATCH] Added solution to problem 4 --- ProjectEulerCS/ProblemSelection.cs | 4 +- ProjectEulerCS/Problems/Problem4.cs | 111 ++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 ProjectEulerCS/Problems/Problem4.cs diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index 1f70c22..7e072a3 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -27,7 +27,8 @@ using System.Collections.Generic; namespace ProjectEulerCS{ public class ProblemSelection{ //Holds the valid problem numbers - private static readonly List _PROBLEM_NUMBERS = new List() { 0, 1, 2, 3 }; + private static readonly List _PROBLEM_NUMBERS = new List() + {0, 1, 2, 3, 4}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -39,6 +40,7 @@ namespace ProjectEulerCS{ case 1: problem = new Problem1(); break; case 2: problem = new Problem2(); break; case 3: problem = new Problem3(); break; + case 4: problem = new Problem4(); break; } return problem; } diff --git a/ProjectEulerCS/Problems/Problem4.cs b/ProjectEulerCS/Problems/Problem4.cs new file mode 100644 index 0000000..b580a09 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem4.cs @@ -0,0 +1,111 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem4.cs +//Matthew Ellison +// Created: 08-23-20 +//Modified: 08-23-20 +//Find the largest palindrome made from the product of two 3-digit numbers +//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; +using System.Linq; + + +namespace ProjectEulerCS{ + public class Problem4 : Problem{ + //Variables + //Static variables + private const int START_NUM = 100; //The first number to be multiplied + private const int END_NUM = 999; //The last number to be multiplied + //Instace variable + private List _palindromes; //Holds all numbers that turn out to be palindromes + public List palindromes{ + get{ + if(!solved){ + throw new Unsolved(); + } + return _palindromes; + } + } + public int largestPalindrom{ + get{ + if(!solved){ + throw new Unsolved(); + } + return _palindromes[_palindromes.Count - 1]; + } + } + + //Constructor + public Problem4() : base("Find the largest palindrome made from the product of two 3-digit numbers"){ + _palindromes = new List(); + } + //Operational funcitons + //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 at the first 3-digit number and check every one up to the last 3-digit number + for(int firstNum = START_NUM;firstNum <= END_NUM;++firstNum){ + //You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100) + for(int secondNum = firstNum;secondNum < END_NUM;++secondNum){ + //Get the product + int product = firstNum * secondNum; + //Change the number into a string + string productString = product.ToString(); + //Reverse the string + string reverseString = new string(productString.ToCharArray().Reverse().ToArray()); + + //If the number and it's reverse are the same it is a palindrom so add it to the list + if(productString == reverseString){ + _palindromes.Add(product); + } + //If it's not a palindrome ignore it and move to the next number + } + } + + //Sort the palindromes so that the last one is the largest + _palindromes.Sort(); + + //Stop the timer + _timer.stop(); + + //Throw a flag to show the problem is solved + solved = true; + + //Save the results + _result = "The largest palindrome is " + _palindromes[_palindromes.Count - 1]; + } + //Reset the problem so it can be run again + public override void reset(){ + base.reset(); + _palindromes.Clear(); + } + } +} + +/* Results: +The largest palindrome is 906609 +It took an average of 51.682 milliseconds to run this problem through 100 iterations +*/