mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2025-12-06 17:23:57 -05:00
Added solution to problem 4
This commit is contained in:
@@ -27,7 +27,8 @@ using System.Collections.Generic;
|
|||||||
namespace ProjectEulerCS{
|
namespace ProjectEulerCS{
|
||||||
public class ProblemSelection{
|
public class ProblemSelection{
|
||||||
//Holds the valid problem numbers
|
//Holds the valid problem numbers
|
||||||
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>() { 0, 1, 2, 3 };
|
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
|
||||||
|
{0, 1, 2, 3, 4};
|
||||||
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
||||||
get { return _PROBLEM_NUMBERS; }
|
get { return _PROBLEM_NUMBERS; }
|
||||||
}
|
}
|
||||||
@@ -39,6 +40,7 @@ namespace ProjectEulerCS{
|
|||||||
case 1: problem = new Problem1(); break;
|
case 1: problem = new Problem1(); break;
|
||||||
case 2: problem = new Problem2(); break;
|
case 2: problem = new Problem2(); break;
|
||||||
case 3: problem = new Problem3(); break;
|
case 3: problem = new Problem3(); break;
|
||||||
|
case 4: problem = new Problem4(); break;
|
||||||
}
|
}
|
||||||
return problem;
|
return problem;
|
||||||
}
|
}
|
||||||
|
|||||||
111
ProjectEulerCS/Problems/Problem4.cs
Normal file
111
ProjectEulerCS/Problems/Problem4.cs
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
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<int> _palindromes; //Holds all numbers that turn out to be palindromes
|
||||||
|
public List<int> 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<int>();
|
||||||
|
}
|
||||||
|
//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
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user