mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Added solution for problem 35
This commit is contained in:
@@ -32,7 +32,7 @@ enum BenchmarkOptions{runSpecific = 1, runAllShort, runAll, exit, size};
|
|||||||
|
|
||||||
|
|
||||||
export class Benchmark{
|
export class Benchmark{
|
||||||
private static tooLong: number[] = [14, 15, 23, 24, 25];
|
private static tooLong: number[] = [14, 15, 23, 24, 25, 35];
|
||||||
//The driver function for the benchmark selection
|
//The driver function for the benchmark selection
|
||||||
public static benchmarkMenu(): void{
|
public static benchmarkMenu(): void{
|
||||||
let selection: BenchmarkOptions = BenchmarkOptions.size;
|
let selection: BenchmarkOptions = BenchmarkOptions.size;
|
||||||
|
|||||||
@@ -57,14 +57,15 @@ import { Problem30 } from "./Problems/Problem30";
|
|||||||
import { Problem31 } from "./Problems/Problem31";
|
import { Problem31 } from "./Problems/Problem31";
|
||||||
import { Problem32 } from "./Problems/Problem32";
|
import { Problem32 } from "./Problems/Problem32";
|
||||||
import { Problem33 } from "./Problems/Problem33";
|
import { Problem33 } from "./Problems/Problem33";
|
||||||
import { Problem67 } from "./Problems/Problem67";
|
|
||||||
import { Problem34 } from "./Problems/Problem34";
|
import { Problem34 } from "./Problems/Problem34";
|
||||||
|
import { Problem35 } from "./Problems/Problem35";
|
||||||
|
import { Problem67 } from "./Problems/Problem67";
|
||||||
|
|
||||||
|
|
||||||
export class ProblemSelection{
|
export class ProblemSelection{
|
||||||
//Holds the valid problem numbers
|
//Holds the valid problem numbers
|
||||||
public static PROBLEM_NUMBERS: number[] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
public static PROBLEM_NUMBERS: number[] = [ 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];
|
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 67];
|
||||||
|
|
||||||
//Returns the problem corresponding to the given problem number
|
//Returns the problem corresponding to the given problem number
|
||||||
public static getProblem(problemNumber: number): Problem{
|
public static getProblem(problemNumber: number): Problem{
|
||||||
@@ -104,6 +105,7 @@ export class ProblemSelection{
|
|||||||
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;
|
||||||
|
|||||||
138
Problems/Problem35.ts
Normal file
138
Problems/Problem35.ts
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
//ProjectEulerTS/Problems/Problem35.ts
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 06-06-21
|
||||||
|
//Modified: 06-06-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/typescriptClasses
|
||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
import { getPrimes } from "../../../Typescript/typescriptClasses/Algorithms";
|
||||||
|
import { Unsolved } from "../Unsolved";
|
||||||
|
import { Problem } from "./Problem";
|
||||||
|
|
||||||
|
|
||||||
|
export class Problem35 extends Problem{
|
||||||
|
//Variables
|
||||||
|
//Static variables
|
||||||
|
private static MAX_NUM: number = 999999; //The largest number that we are checking for primes
|
||||||
|
//private static MAX_NUM: number = 100;
|
||||||
|
//Instance variables
|
||||||
|
private primes: number[]; //The primes below MAX_NUM
|
||||||
|
private circularPrimes: number[]; //The circular primes below MAX_NUM
|
||||||
|
//Functions
|
||||||
|
//Returns a list of all rotations of a string passed to it
|
||||||
|
private getRotations(str: string): string[]{
|
||||||
|
let rotations: string[] = [];
|
||||||
|
rotations.push(str);
|
||||||
|
for(let cnt = 1;cnt < str.length;++cnt){
|
||||||
|
str = str.substring(1) + str[0];
|
||||||
|
rotations.push(str);
|
||||||
|
}
|
||||||
|
return rotations;
|
||||||
|
}
|
||||||
|
//Constructor
|
||||||
|
public constructor(){
|
||||||
|
super("How many circular primes are there below one million?");
|
||||||
|
this.primes = [];
|
||||||
|
this.circularPrimes = [];
|
||||||
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
|
public solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(this.solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Start the timer
|
||||||
|
this.timer.start();
|
||||||
|
|
||||||
|
//Get all primes under 1,000,000
|
||||||
|
this.primes = getPrimes(Problem35.MAX_NUM);
|
||||||
|
//Go through all primes, get all their rotations, and check if htose numbers are also primes
|
||||||
|
for(let cnt = 0;cnt < this.primes.length;++cnt){
|
||||||
|
let prime = this.primes[cnt];
|
||||||
|
let allRotationsPrime: boolean = true;
|
||||||
|
//Get all of the rotations of the prime and see if they are also prime
|
||||||
|
let rotations: string[] = this.getRotations(prime.toString());
|
||||||
|
for(let rotCnt = 0;rotCnt < rotations.length;++rotCnt){
|
||||||
|
let rotation = rotations[rotCnt];
|
||||||
|
let p: number = parseInt(rotation);
|
||||||
|
if(!this.primes.includes(p)){
|
||||||
|
allRotationsPrime = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//If all rotations are prime add it to the list of circular primes
|
||||||
|
if(allRotationsPrime){
|
||||||
|
this.circularPrimes.push(prime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Stop the timer
|
||||||
|
this.timer.stop();
|
||||||
|
|
||||||
|
//Throw a flag to show the problem is solved
|
||||||
|
this.solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public reset(){
|
||||||
|
super.reset();
|
||||||
|
this.primes = [];
|
||||||
|
this.circularPrimes = [];
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns a string with the solution to the problem
|
||||||
|
public getResult(): string{
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!this.solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return `The number of all circular prime numbers under ${Problem35.MAX_NUM} is ${this.circularPrimes.length}`;
|
||||||
|
}
|
||||||
|
//Returns the array of primes < MAX_NUM
|
||||||
|
public getPrimes(): number[]{
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!this.solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return this.primes;
|
||||||
|
}
|
||||||
|
//Returns the array of circular primes < MAX_NUM
|
||||||
|
public getCircularPrimes(): number[]{
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!this.solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return this.circularPrimes;
|
||||||
|
}
|
||||||
|
//Returns the number of circular primes < MAX_NUM
|
||||||
|
public getNumCircularPrimes(): number{
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!this.solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return this.circularPrimes.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Results:
|
||||||
|
The number of all circular prime numbers under 999999 is 55
|
||||||
|
It took an average of 7.204 seconds to run this problem through 100 iterations
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user