mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Added solution for problem 35
This commit is contained in:
@@ -57,14 +57,15 @@ pub mod Problem31;
|
||||
pub mod Problem32;
|
||||
pub mod Problem33;
|
||||
pub mod Problem34;
|
||||
pub mod Problem35;
|
||||
pub mod Problem67;
|
||||
|
||||
|
||||
pub static problemNumbers: [u32; 36] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
pub static problemNumbers: [u32; 37] = [ 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];
|
||||
pub static tooLong: [u32; 7] = [3, 5, 15, 23, 24, 25, 27];
|
||||
30, 31, 32, 33, 34, 35, 67];
|
||||
pub static tooLong: [u32; 8] = [3, 5, 15, 23, 24, 25, 27, 35];
|
||||
|
||||
pub fn solveProblem(problemNumber: u32, description: bool, solve: bool) -> Answer::Answer{
|
||||
let mut answer = Answer::Answer::new("".to_string(), "".to_string(), 0);
|
||||
@@ -344,6 +345,14 @@ pub fn solveProblem(problemNumber: u32, description: bool, solve: bool) -> Answe
|
||||
answer = Problem34::solve();
|
||||
}
|
||||
}
|
||||
else if(problemNumber == 35){
|
||||
if(description){
|
||||
println!("{}", Problem35::getDescription());
|
||||
}
|
||||
if(solve){
|
||||
answer = Problem35::solve();
|
||||
}
|
||||
}
|
||||
else if(problemNumber == 67){
|
||||
if(description){
|
||||
println!("{}", Problem67::getDescription());
|
||||
|
||||
88
src/Problems/Problem35.rs
Normal file
88
src/Problems/Problem35.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
//ProjectEulerRust/src/Problems/Problem35.rs
|
||||
//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/RustClasses
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
|
||||
|
||||
extern crate myClasses;
|
||||
use crate::Problems::Answer::Answer;
|
||||
|
||||
pub fn getDescription() -> String{
|
||||
"How many circular primes are there below one million?".to_string()
|
||||
}
|
||||
|
||||
pub fn solve() -> Answer{
|
||||
//Variables
|
||||
//Static variables
|
||||
let maxNum = 999999; //The largest number that we are checking for primes
|
||||
//Instance variables
|
||||
let mut circularPrimes = Vec::<i64>::new();
|
||||
|
||||
//Start the timer
|
||||
let mut timer = myClasses::Stopwatch::Stopwatch::new();
|
||||
timer.start();
|
||||
|
||||
//Get all primes under 1,000,000
|
||||
let primes = myClasses::Algorithms::getPrimes(maxNum);
|
||||
//Go through all primes, get all their rotations, and check if those numbers are also primes
|
||||
for num in &primes{
|
||||
let prime = *num;
|
||||
let mut allRotationsPrime = true;
|
||||
//Get all of the rotations of the prime and see if they are also prime
|
||||
let rotations = getRotations(&prime.to_string());
|
||||
for rotation in rotations{
|
||||
let p = rotation.parse::<i64>().unwrap();
|
||||
if(!primes.contains(&p)){
|
||||
allRotationsPrime = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//If all rotations are prime add it to the list of circular primes
|
||||
if(allRotationsPrime){
|
||||
circularPrimes.push(prime);
|
||||
}
|
||||
}
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
return Answer::new(format!("The number of all circular prime numbers under {} is {}", maxNum, circularPrimes.len()), timer.getString(), timer.getNano());
|
||||
}
|
||||
|
||||
fn getRotations(par1: &String) -> Vec::<String>{
|
||||
let mut string = String::from(par1);
|
||||
let stringCount = String::from(par1).chars().count();
|
||||
let mut rotations = Vec::<String>::new();
|
||||
rotations.push(string.to_string());
|
||||
for _cnt in 1 as usize .. stringCount{
|
||||
let mut tempString = string[1..].to_string();
|
||||
let tempString2 = string[..1].to_owned();
|
||||
tempString.push_str(&tempString2);
|
||||
string = String::from(tempString);
|
||||
rotations.push(string.to_string());
|
||||
}
|
||||
return rotations;
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The number of all circular prime numbers under 999999 is 55
|
||||
It took an average of 3.576 seconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user