mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2026-02-03 19:52:36 -05:00
Added solution to problem 35
This commit is contained in:
@@ -28,7 +28,7 @@ namespace ProjectEulerCS{
|
|||||||
public class Benchmark{
|
public class Benchmark{
|
||||||
private enum BenchmarkOptions { RunSpecific = 1, RunAllShort, RunAll, Exit, Size };
|
private enum BenchmarkOptions { RunSpecific = 1, RunAllShort, RunAll, Exit, Size };
|
||||||
private static readonly List<int> tooLong = new List<int>()
|
private static readonly List<int> tooLong = new List<int>()
|
||||||
{5, 15, 23, 24};
|
{5, 15, 23, 24, 35};
|
||||||
|
|
||||||
//The driver function for the benchmark selection
|
//The driver function for the benchmark selection
|
||||||
public static void BenchmarkMenu(){
|
public static void BenchmarkMenu(){
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEuler/ProjectEulerCS/src/ProblemSelection.cs
|
//ProjectEuler/ProjectEulerCS/src/ProblemSelection.cs
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 08-21-20
|
// Created: 08-21-20
|
||||||
//Modified: 08-27-20
|
//Modified: 06-05-21
|
||||||
//This class holds all of the functions needed to handle a problem
|
//This class holds all of the functions needed to handle a problem
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2020 Matthew Ellison
|
Copyright (C) 2020 Matthew Ellison
|
||||||
@@ -33,7 +33,7 @@ namespace ProjectEulerCS{
|
|||||||
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
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<int> PROBLEM_NUMBERS{
|
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
||||||
get { return _PROBLEM_NUMBERS; }
|
get { return _PROBLEM_NUMBERS; }
|
||||||
}
|
}
|
||||||
@@ -76,6 +76,7 @@ namespace ProjectEulerCS{
|
|||||||
case 32: problem = new Problem32(); break;
|
case 32: problem = new Problem32(); break;
|
||||||
case 33: problem = new Problem33(); break;
|
case 33: problem = new Problem33(); break;
|
||||||
case 34: problem = new Problem34(); break;
|
case 34: problem = new Problem34(); break;
|
||||||
|
case 35: problem = new Problem35(); break;
|
||||||
case 67: problem = new Problem67(); break;
|
case 67: problem = new Problem67(); break;
|
||||||
}
|
}
|
||||||
return problem;
|
return problem;
|
||||||
|
|||||||
@@ -22,7 +22,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
|
||||||
@@ -75,7 +74,7 @@ namespace ProjectEulerCS.Problems{
|
|||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
public override void Solve(){
|
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){
|
if(solved){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
138
ProjectEulerCS/Problems/Problem35.cs
Normal file
138
ProjectEulerCS/Problems/Problem35.cs
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
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<int> primes; //The primes below MAX_NUM
|
||||||
|
private readonly List<int> 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<int> Primes{
|
||||||
|
get{
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return primes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Returns the vector of circular primes < MAX_NUM
|
||||||
|
public List<int> 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<string> GetRotations(string str){
|
||||||
|
List<string> rotations = new List<string>{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<int>();
|
||||||
|
circularPrimes = new List<int>();
|
||||||
|
}
|
||||||
|
//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<string> 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
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user