Added solution to problem 37

This commit is contained in:
2021-07-01 11:36:28 -04:00
parent a3efaf60c2
commit d01251ed02
3 changed files with 201 additions and 39 deletions

View File

@@ -33,7 +33,7 @@ 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, 67};
30, 31, 32, 33, 34, 35, 36, 37, 67};
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
get { return _PROBLEM_NUMBERS; }
}
@@ -42,43 +42,44 @@ namespace ProjectEulerCS{
public static Problem GetProblem(int problemNumber){
Problem problem = null;
switch(problemNumber){
case 1: problem = new Problem1(); break;
case 2: problem = new Problem2(); break;
case 3: problem = new Problem3(); break;
case 4: problem = new Problem4(); break;
case 5: problem = new Problem5(); break;
case 6: problem = new Problem6(); break;
case 7: problem = new Problem7(); break;
case 8: problem = new Problem8(); break;
case 9: problem = new Problem9(); break;
case 10: problem = new Problem10(); break;
case 11: problem = new Problem11(); break;
case 12: problem = new Problem12(); break;
case 13: problem = new Problem13(); break;
case 14: problem = new Problem14(); break;
case 15: problem = new Problem15(); break;
case 16: problem = new Problem16(); break;
case 17: problem = new Problem17(); break;
case 18: problem = new Problem18(); break;
case 19: problem = new Problem19(); break;
case 20: problem = new Problem20(); break;
case 21: problem = new Problem21(); break;
case 22: problem = new Problem22(); break;
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 27: problem = new Problem27(); break;
case 28: problem = new Problem28(); break;
case 29: problem = new Problem29(); break;
case 30: problem = new Problem30(); break;
case 31: problem = new Problem31(); break;
case 32: problem = new Problem32(); break;
case 33: problem = new Problem33(); break;
case 34: problem = new Problem34(); break;
case 35: problem = new Problem35(); break;
case 36: problem = new Problem36(); break;
case 67: problem = new Problem67(); break;
case 1 : problem = new Problem1(); break;
case 2 : problem = new Problem2(); break;
case 3 : problem = new Problem3(); break;
case 4 : problem = new Problem4(); break;
case 5 : problem = new Problem5(); break;
case 6 : problem = new Problem6(); break;
case 7 : problem = new Problem7(); break;
case 8 : problem = new Problem8(); break;
case 9 : problem = new Problem9(); break;
case 10 : problem = new Problem10(); break;
case 11 : problem = new Problem11(); break;
case 12 : problem = new Problem12(); break;
case 13 : problem = new Problem13(); break;
case 14 : problem = new Problem14(); break;
case 15 : problem = new Problem15(); break;
case 16 : problem = new Problem16(); break;
case 17 : problem = new Problem17(); break;
case 18 : problem = new Problem18(); break;
case 19 : problem = new Problem19(); break;
case 20 : problem = new Problem20(); break;
case 21 : problem = new Problem21(); break;
case 22 : problem = new Problem22(); break;
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 27 : problem = new Problem27(); break;
case 28 : problem = new Problem28(); break;
case 29 : problem = new Problem29(); break;
case 30 : problem = new Problem30(); break;
case 31 : problem = new Problem31(); break;
case 32 : problem = new Problem32(); break;
case 33 : problem = new Problem33(); break;
case 34 : problem = new Problem34(); break;
case 35 : problem = new Problem35(); break;
case 36 : problem = new Problem36(); break;
case 37 : problem = new Problem37(); break;
case 67 : problem = new Problem67(); break;
}
return problem;
}

View File

@@ -31,7 +31,7 @@ namespace ProjectEulerCS.Problems{
//Static variables
private static readonly int MAX_NUM = 999999; //The largest number that will be checked
//Instance variables
private List<int> palindromes; //All numbers that are palindromes in base 10 and 2
private readonly List<int> palindromes; //All numbers that are palindromes in base 10 and 2
private int sum; //The sum of all elements in the list of palindromes
//Gets
//The results of the problem

View File

@@ -0,0 +1,161 @@
//ProjectEuler/ProjectEulerCS/src/Problems/Problem37.cs
//Matthew Ellison
// Created: 06-30-21
//Modified: 06-30-21
//Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted).
//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/>.
*/
using System.Collections.Generic;
namespace ProjectEulerCS.Problems{
public class Problem37 : Problem{
//Variables
//Static variables
private 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
//Gets
//The results of the problem
public override string Result{
get{
if(!solved){
throw new Unsolved();
}
return $"The sum of all left and right truncatable primes is {sum}";
}
}
public List<long> TruncatablePrimes{
get{
if(!solved){
throw new Unsolved();
}
return truncPrimes;
}
}
public long SumOfTruncatablePrimes{
get{
if(!solved){
throw new Unsolved();
}
return sum;
}
}
//Functions
//Constructor
public Problem37() : base("Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted)."){
truncPrimes = new List<long>();
sum = 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();
//Create the sieve and get the first prime number
IEnumerator<long> sieve = mee.Algorithms.SieveOfEratosthenes().GetEnumerator();
sieve.MoveNext();
//Loop through the sieve until you get to the LAST_PRIME_BEFORE_CHECK
while(sieve.Current < LAST_PRIME_BEFORE_CHECK){
sieve.MoveNext();
}
//Loop until trucPrimes contains 11 elements
while(truncPrimes.Count < 11){
bool isTruncPrime = true;
//Get the next prime
sieve.MoveNext();
//Convert the prime to a string
string primeString = sieve.Current.ToString();
//If the string contains an even digit move to the next prime
for(int strLoc = 0;(strLoc < primeString.Length) && (isTruncPrime);++strLoc){
//Allow 2 to be the first digit
if((strLoc == 0) && (primeString[strLoc] == '2')){
continue;
}
switch(primeString[strLoc]){
case '0' :
case '2' :
case '4' :
case '6' :
case '8' : isTruncPrime = false; break;
}
}
//Start removing digits from the left and see if the number stays prime
if(isTruncPrime){
for(int truncLoc = 1;truncLoc < primeString.Length;++truncLoc){
//Create a substring of the prime, removing the needed digits from the left
string primeSubstring = primeString[truncLoc..];
//Convert the string to an int and see if the number is still prime
long newPrime = long.Parse(primeSubstring);
if(!mee.Algorithms.IsPrime(newPrime)){
isTruncPrime = false;
break;
}
}
}
//Start removing digits from the right and see if the number stays prime
if(isTruncPrime){
for(int truncLoc = 1;truncLoc < primeString.Length;++truncLoc){
//Create a substring of the prime, removing the needed digits from the right
string primeSubstring = primeString.Substring(0, primeString.Length - truncLoc);
//Conver the string to an int and see if the number is still prime
long newPrime = long.Parse(primeSubstring);
if(!mee.Algorithms.IsPrime(newPrime)){
isTruncPrime = false;
break;
}
}
}
//If the number remained prime through all operations add it to the vector
if(isTruncPrime){
truncPrimes.Add(sieve.Current);
}
}
//Get the sum of all elements in the trucPrimes vector
sum = mee.Algorithms.GetSum(truncPrimes);
//Stop the timer
timer.Stop();
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the porblem so it can be run again
public override void Reset(){
base.Reset();
truncPrimes.Clear();
sum = 0;
}
}
}
/* Results:
The sum of all left and right truncatable primes is 748317
It took an average of 82.833 milliseconds to run this problem through 100 iterations
*/