Added solution to problem 26

This commit is contained in:
2020-06-17 20:43:47 -04:00
parent 0add7eb18b
commit 49bd5dc644
3 changed files with 102 additions and 2 deletions

View File

@@ -48,3 +48,4 @@ pub mod Problem22;
pub mod Problem23; pub mod Problem23;
pub mod Problem24; pub mod Problem24;
pub mod Problem25; pub mod Problem25;
pub mod Problem26;

92
src/Problems/Problem26.rs Normal file
View File

@@ -0,0 +1,92 @@
//ProjectEulerRust/src/Problems/Problems26.rs
//Matthew Ellison
// Created: 06-17-20
//Modified: 06-17-20
//Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses
/*
Copyright (C) 2020 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{
"Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.".to_string()
}
pub fn solve() -> Answer{
let TOP_NUM = 999;
//The length of the longest cycle
let mut longestCycle = 0;
//The starting denominator of the longest cycle
let mut longestNumber = 0;
//Start the timer
let mut timer = myClasses::Stopwatch::Stopwatch::new();
timer.start();
//Start with 1/2 and find out how long the longest cycle is by checking the remainders
//Loop through every number from 2-999 and use it for the denominator
for denominator in 2..=TOP_NUM{
let mut denomList = Vec::<i64>::new();
let mut endFound = false; //Whether we have found an end to the number (either a cycle or a 0 for remainder)
let mut cycleFound = false; //Whether a cycle was detected
let mut numerator = 1; //The numerator that will be divided. Always starts at 1
while(!endFound){
//Get the remainder after the division
let remainder = numerator % denominator;
//Check if the remainder is 0
//If it is set the flag
if(remainder == 0){
endFound = true;
}
//Check if the remainder is in the list
//If it is in the list, set the appropriate flags
else if(denomList.contains(&remainder)){
endFound = true;
cycleFound = true;
}
//Else add it to the list
else{
denomList.push(remainder);
}
//Multiply the remainder by 10 to continue finding the next remainder
numerator = remainder * 10;
}
//If a cycle was found check the size of the list against the largest cycle
if(cycleFound){
//If it is larger than the largest, set it as the new largest
if(denomList.len() > longestCycle){
longestCycle = denomList.len();
longestNumber = denominator;
}
}
}
//Stop the timer
timer.stop();
//Return the results
return Answer::new(format!("The longest cycle is {} digits long\nIt started with the number {}", longestCycle, longestNumber), timer.getString());
}
/* Results:
The longest cycle is 982 digits long
It started with the number 983
It took 6.518 milliseconds to solve this problem
*/

View File

@@ -33,9 +33,9 @@ mod Problems;
#[derive(PartialEq)] #[derive(PartialEq)]
enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE}
static problemNumbers: [u32; 26] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, static problemNumbers: [u32; 27] = [ 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]; 20, 21, 22, 23, 24, 25, 26];
fn main(){ fn main(){
let mut selection = Selections::EMPTY; let mut selection = Selections::EMPTY;
@@ -232,6 +232,10 @@ fn solveProblem(problemNumber: u32){
println!("{}", Problems::Problem25::getDescription()); println!("{}", Problems::Problem25::getDescription());
println!("{}", Problems::Problem25::solve()); println!("{}", Problems::Problem25::solve());
} }
else if(problemNumber == 26){
println!("{}", Problems::Problem26::getDescription());
println!("{}", Problems::Problem26::solve());
}
} }
fn descriptionMenu(){ fn descriptionMenu(){
//Give some extra space to print the description //Give some extra space to print the description
@@ -332,6 +336,9 @@ fn printDescription(problemNumber: u32){
else if(problemNumber == 25){ else if(problemNumber == 25){
println!("{}", Problems::Problem25::getDescription()); println!("{}", Problems::Problem25::getDescription());
} }
else if(problemNumber == 26){
println!("{}", Problems::Problem26::getDescription());
}
} }
fn getProblemNumber() -> u32{ fn getProblemNumber() -> u32{
println!("Enter a problem number: "); println!("Enter a problem number: ");