Added solution to problem 38

This commit is contained in:
2021-10-22 19:28:30 -04:00
parent 7d4455dd5b
commit 4e99fa1b8b
3 changed files with 122 additions and 6 deletions

View File

@@ -1,10 +1,10 @@
//ProjectEuler/ProjectEulerCS/src/ProblemSelection.cs
//Matthew Ellison
// Created: 08-21-20
//Modified: 06-05-21
//Modified: 10-11-21
//This class holds all of the functions needed to handle a problem
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 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
@@ -33,8 +33,8 @@ namespace ProjectEulerCS{
{ 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, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 67};
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
30, 31, 32, 33, 34, 35, 36, 37, 38, 67};
public static List<int> PROBLEM_NUMBERS{
get { return _PROBLEM_NUMBERS; }
}
@@ -79,6 +79,7 @@ namespace ProjectEulerCS{
case 35 : problem = new Problem35(); break;
case 36 : problem = new Problem36(); break;
case 37 : problem = new Problem37(); break;
case 38 : problem = new Problem38(); break;
case 67 : problem = new Problem67(); break;
}
return problem;

View File

@@ -29,7 +29,7 @@ namespace ProjectEulerCS.Problems{
public class Problem37 : Problem{
//Variables
//Static variables
private readonly long LAST_PRIME_BEFORE_CHECK = 7;
private static readonly long LAST_PRIME_BEFORE_CHECK = 7;
//Instance variables
private readonly List<long> truncPrimes; //All numbers that are truncatable primes
private long sum; //The sum of all elements in truncPrimes
@@ -141,7 +141,7 @@ namespace ProjectEulerCS.Problems{
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the porblem so it can be run again
//Reset the problem so it can be run again
public override void Reset(){
base.Reset();
truncPrimes.Clear();

View File

@@ -0,0 +1,115 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem38.cs
//Matthew Ellison
// Created: 10-11-21
//Modified: 10-11-21
//What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
/*
Copyright (C) 2021 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/>.
*/
namespace ProjectEulerCS.Problems{
public class Problem38 : Problem{
//Variables
//Static variables
private static readonly long HIGHEST_POSSIBLE_NUM = 9999; //The highest number that needs to be checked for a 1-9 pandigital
//Instance variables
private long largestNum; //The number passed to the executeFormula function that returns the largest pandigital
private long pandigital; //The largest pandigital number found
//Gets
//The results of the problem
public override string Result{
get{
SolvedCheck("result");
return $"The largest appended product pandigital is {pandigital}";
}
}
public long LargestNum{
get{
SolvedCheck("largest number");
return largestNum;
}
}
public long Pandigital{
get{
SolvedCheck("pandigital");
return pandigital;
}
}
//Functions
//Constructor
public Problem38() : base("What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1"){
largestNum = 0;
pandigital = 0;
}
//Operational functions
//Take the number and add its multiples to a string to return
private string ExecuteFormula(int num){
//Turn the current number into a string
string numStr = num.ToString();
int cnt = 2;
//Multiply the number and append the product to the string until you have one long enough
do{
numStr += (num * cnt).ToString();
++cnt;
}while(numStr.Length < 9);
return numStr;
}
//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 from 1 -> HIGHEST_POSSIBLE_NUM checking for pandigitals
for(int cnt = 1;cnt <= HIGHEST_POSSIBLE_NUM;++cnt){
//Get the string from the formula
string numStr = ExecuteFormula(cnt);
long panNum = long.Parse(numStr);
//If the number is pandigital save it as the highest number
if(mee.StringAlgorithms.IsPandigital(numStr) && (panNum > pandigital)){
largestNum = cnt;
pandigital = panNum;
}
}
//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();
largestNum = 0;
pandigital = 0;
}
}
}
/* Results:
The largest appended product pandigital is 932718654
It took 4.055 milliseconds to solve this problem.
*/