From f2c3a7ade1f904be2bcf19d8f6bc8e6e0b200d42 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 6 Jun 2021 12:19:03 -0400 Subject: [PATCH] Added solution to problem 35 --- ProjectEulerCS/Benchmark.cs | 2 +- ProjectEulerCS/ProblemSelection.cs | 5 +- ProjectEulerCS/Problems/Problem34.cs | 3 +- ProjectEulerCS/Problems/Problem35.cs | 138 +++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 ProjectEulerCS/Problems/Problem35.cs diff --git a/ProjectEulerCS/Benchmark.cs b/ProjectEulerCS/Benchmark.cs index e0a7960..eca0098 100644 --- a/ProjectEulerCS/Benchmark.cs +++ b/ProjectEulerCS/Benchmark.cs @@ -28,7 +28,7 @@ namespace ProjectEulerCS{ public class Benchmark{ private enum BenchmarkOptions { RunSpecific = 1, RunAllShort, RunAll, Exit, Size }; private static readonly List tooLong = new List() - {5, 15, 23, 24}; + {5, 15, 23, 24, 35}; //The driver function for the benchmark selection public static void BenchmarkMenu(){ diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index 16f5273..0dda64d 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/ProblemSelection.cs //Matthew Ellison // Created: 08-21-20 -//Modified: 08-27-20 +//Modified: 06-05-21 //This class holds all of the functions needed to handle a problem /* Copyright (C) 2020 Matthew Ellison @@ -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, 67}; + 30, 31, 32, 33, 34, 35, 67}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -76,6 +76,7 @@ namespace ProjectEulerCS{ 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 67: problem = new Problem67(); break; } return problem; diff --git a/ProjectEulerCS/Problems/Problem34.cs b/ProjectEulerCS/Problems/Problem34.cs index 3e2a396..406143a 100644 --- a/ProjectEulerCS/Problems/Problem34.cs +++ b/ProjectEulerCS/Problems/Problem34.cs @@ -22,7 +22,6 @@ */ -using System; using System.Collections.Generic; @@ -75,7 +74,7 @@ namespace ProjectEulerCS.Problems{ //Operational functions //Solve the problem public override void Solve(){ - //If the porblem has already been solved do nothing and end the function + //If the problem has already been solved do nothing and end the function if(solved){ return; } diff --git a/ProjectEulerCS/Problems/Problem35.cs b/ProjectEulerCS/Problems/Problem35.cs new file mode 100644 index 0000000..87c6208 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem35.cs @@ -0,0 +1,138 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem35.cs +//Matthew Ellison +// Created: 06-05-21 +//Modified: 06-05-21 +//How many circular primes are there below one million? +//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 . +*/ + + +using System.Collections.Generic; + + +namespace ProjectEulerCS.Problems{ + public class Problem35 : Problem{ + //Variables + //Static variables + private static readonly int MAX_NUM = 999999; //The largest number that we are checking for primes + //Instance variables + private List primes; //The primes below MAX_NUM + private readonly List circularPrimes; //The circular primes below MAX_NUM + //Gets + //The results of the problem + public override string Result{ + get{ + if(!solved){ + throw new Unsolved(); + } + return $"The number of all circular prime numbers under {MAX_NUM} is {circularPrimes.Count}"; + } + } + //Returns the vector of primes < MAX_NUM + public List Primes{ + get{ + if(!solved){ + throw new Unsolved(); + } + return primes; + } + } + //Returns the vector of circular primes < MAX_NUM + public List CircularPrimes{ + get{ + if(!solved){ + throw new Unsolved(); + } + return circularPrimes; + } + } + //Returns the number of circular primes + public int NumCircularPrimes{ + get{ + if(!solved){ + throw new Unsolved(); + } + return circularPrimes.Count; + } + } + + //Functions + //Returns a list of all rotations of a string passed to it + private List GetRotations(string str){ + List rotations = new List{str}; + for(int cnt = 1;cnt < str.Length;++cnt){ + str = str[1..] + str[0]; + rotations.Add(str); + } + return rotations; + } + //Constructor + public Problem35() : base("How many circular primes are there below one million?"){ + primes = new List(); + circularPrimes = new List(); + } + //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(); + + //Get all primes under 1,000,000 + primes = mee.Algorithms.GetPrimes(MAX_NUM); + //Go through all primes, get all their rotations, and check if those numbers are also primes + foreach(int prime in primes){ + bool allRotationsPrime = true; + //Get all of the rotations of the prime and see if they are also prime + List rotations = GetRotations(prime.ToString()); + foreach(string rotation in rotations){ + int p = int.Parse(rotation); + if(!primes.Contains(p)){ + allRotationsPrime = false; + break; + } + } + //If all rotations are prime add it to the list of circular primes + if(allRotationsPrime){ + circularPrimes.Add(prime); + } + } + + //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(); + primes.Clear(); + circularPrimes.Clear(); + } + } +} + +/* Results: +The number of all circular prime numbers under 999999 is 55 +It took an average of 1.946 seconds to run this problem through 100 iterations +*/