From baab41f5c06f35580fe7347fd891267208b7d235 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 22 Jul 2020 13:50:34 -0400 Subject: [PATCH] Updated to allow benchmarking --- src/Problems.rs | 296 +++++++++++++++++++++++++ src/Problems/Answer.rs | 16 +- src/Problems/Problem1.rs | 6 +- src/Problems/Problem10.rs | 6 +- src/Problems/Problem11.rs | 6 +- src/Problems/Problem12.rs | 6 +- src/Problems/Problem13.rs | 6 +- src/Problems/Problem14.rs | 6 +- src/Problems/Problem15.rs | 6 +- src/Problems/Problem16.rs | 6 +- src/Problems/Problem17.rs | 6 +- src/Problems/Problem18.rs | 6 +- src/Problems/Problem19.rs | 6 +- src/Problems/Problem2.rs | 6 +- src/Problems/Problem20.rs | 6 +- src/Problems/Problem21.rs | 6 +- src/Problems/Problem22.rs | 6 +- src/Problems/Problem23.rs | 6 +- src/Problems/Problem24.rs | 6 +- src/Problems/Problem25.rs | 6 +- src/Problems/Problem26.rs | 6 +- src/Problems/Problem27.rs | 6 +- src/Problems/Problem28.rs | 6 +- src/Problems/Problem29.rs | 6 +- src/Problems/Problem3.rs | 6 +- src/Problems/Problem30.rs | 6 +- src/Problems/Problem31.rs | 6 +- src/Problems/Problem4.rs | 6 +- src/Problems/Problem5.rs | 6 +- src/Problems/Problem6.rs | 6 +- src/Problems/Problem67.rs | 6 +- src/Problems/Problem7.rs | 6 +- src/Problems/Problem8.rs | 6 +- src/Problems/Problem9.rs | 8 +- src/main.rs | 449 ++++++++++++++++---------------------- 35 files changed, 590 insertions(+), 365 deletions(-) diff --git a/src/Problems.rs b/src/Problems.rs index 319498d..40e22d7 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -55,3 +55,299 @@ pub mod Problem29; pub mod Problem30; pub mod Problem31; pub mod Problem67; + + +pub static problemNumbers: [u32; 33] = [ 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, 67]; +pub static tooLong: [u32; 7] = [3, 5, 15, 23, 24, 25, 27]; + +pub fn solveProblem(problemNumber: u32, description: bool, solve: bool) -> Answer::Answer{ + let mut answer = Answer::Answer::new("".to_string(), "".to_string(), 0); + //This is ever used, it's just to suppress the warning about getTime not being used in Answer + if(problemNumber == 0){ + println!("In 0 {}", answer.getTime()); + } + if(problemNumber == 1){ + if(description){ + println!("{}", Problem1::getDescription()); + } + if(solve){ + answer = Problem1::solve(); + } + } + else if(problemNumber == 2){ + if(description){ + println!("{}", Problem2::getDescription()); + } + if(solve){ + answer = Problem2::solve(); + } + } + else if(problemNumber == 3){ + if(description){ + println!("{}", Problem3::getDescription()); + } + if(solve){ + answer = Problem3::solve(); + } + } + else if(problemNumber == 4){ + if(description){ + println!("{}", Problem4::getDescription()); + } + if(solve){ + answer = Problem4::solve(); + } + } + else if(problemNumber == 5){ + if(description){ + println!("{}", Problem5::getDescription()); + } + if(solve){ + answer = Problem5::solve(); + } + } + else if(problemNumber == 6){ + if(description){ + println!("{}", Problem6::getDescription()); + } + if(solve){ + answer = Problem6::solve(); + } + } + else if(problemNumber == 7){ + if(description){ + println!("{}", Problem7::getDescription()); + } + if(solve){ + answer = Problem7::solve(); + } + } + else if(problemNumber == 8){ + if(description){ + println!("{}", Problem8::getDescription()); + } + if(solve){ + answer = Problem8::solve(); + } + } + else if(problemNumber == 9){ + if(description){ + println!("{}", Problem9::getDescription()); + } + if(solve){ + answer = Problem9::solve(); + } + } + else if(problemNumber == 10){ + if(description){ + println!("{}", Problem10::getDescription()); + } + if(solve){ + answer = Problem10::solve(); + } + } + else if(problemNumber == 11){ + if(description){ + println!("{}", Problem11::getDescription()); + } + if(solve){ + answer = Problem11::solve(); + } + } + else if(problemNumber == 12){ + if(description){ + println!("{}", Problem12::getDescription()); + } + if(solve){ + answer = Problem12::solve(); + } + } + else if(problemNumber == 13){ + if(description){ + println!("{}", Problem13::getDescription()); + } + if(solve){ + answer = Problem13::solve(); + } + } + else if(problemNumber == 14){ + if(description){ + println!("{}", Problem14::getDescription()); + } + if(solve){ + answer = Problem14::solve(); + } + } + else if(problemNumber == 15){ + if(description){ + println!("{}", Problem15::getDescription()); + } + if(solve){ + answer = Problem15::solve(); + } + } + else if(problemNumber == 16){ + if(description){ + println!("{}", Problem16::getDescription()); + } + if(solve){ + answer = Problem16::solve(); + } + } + else if(problemNumber == 17){ + if(description){ + println!("{}", Problem17::getDescription()); + } + if(solve){ + answer = Problem17::solve(); + } + } + else if(problemNumber == 18){ + if(description){ + println!("{}", Problem18::getDescription()); + } + if(solve){ + answer = Problem18::solve(); + } + } + else if(problemNumber == 19){ + if(description){ + println!("{}", Problem19::getDescription()); + } + if(solve){ + answer = Problem19::solve(); + } + } + else if(problemNumber == 20){ + if(description){ + println!("{}", Problem20::getDescription()); + } + if(solve){ + answer = Problem20::solve(); + } + } + else if(problemNumber == 21){ + if(description){ + println!("{}", Problem21::getDescription()); + } + if(solve){ + answer = Problem21::solve(); + } + } + else if(problemNumber == 22){ + if(description){ + println!("{}", Problem22::getDescription()); + } + if(solve){ + answer = Problem22::solve(); + } + } + else if(problemNumber == 23){ + if(description){ + println!("{}", Problem23::getDescription()); + } + if(solve){ + answer = Problem23::solve(); + } + } + else if(problemNumber == 24){ + if(description){ + println!("{}", Problem24::getDescription()); + } + if(solve){ + answer = Problem24::solve(); + } + } + else if(problemNumber == 25){ + if(description){ + println!("{}", Problem25::getDescription()); + } + if(solve){ + answer = Problem25::solve(); + } + } + else if(problemNumber == 26){ + if(description){ + println!("{}", Problem26::getDescription()); + } + if(solve){ + answer = Problem26::solve(); + } + } + else if(problemNumber == 27){ + if(description){ + println!("{}", Problem27::getDescription()); + } + if(solve){ + answer = Problem27::solve(); + } + } + else if(problemNumber == 28){ + if(description){ + println!("{}", Problem28::getDescription()); + } + if(solve){ + answer = Problem28::solve(); + } + } + else if(problemNumber == 29){ + if(description){ + println!("{}", Problem29::getDescription()); + } + if(solve){ + answer = Problem29::solve(); + } + } + else if(problemNumber == 30){ + if(description){ + println!("{}", Problem30::getDescription()); + } + if(solve){ + answer = Problem30::solve(); + } + } + else if(problemNumber == 31){ + if(description){ + println!("{}", Problem31::getDescription()); + } + if(solve){ + answer = Problem31::solve(); + } + } + else if(problemNumber == 67){ + if(description){ + println!("{}", Problem67::getDescription()); + } + if(solve){ + answer = Problem67::solve(); + } + } + return answer; +} + +pub fn getProblemNumber() -> u32{ + println!("Enter a problem number: "); + //Setup reading from the console + let mut problemNumberStr = String::new(); + std::io::stdin().read_line(&mut problemNumberStr).ok(); + //Convert the read to an int + let mut problemNumber = problemNumberStr.trim().parse::().unwrap(); + + //Loop through all valid problem numbers and see if the one entered matches it + while(!problemNumbers.contains(&problemNumber)){ + println!("That is an invalid problem number!\nEnter a problem number: "); + std::io::stdin().read_line(&mut problemNumberStr).ok(); + problemNumber = problemNumberStr.trim().parse::().unwrap(); + } + return problemNumber; +} +pub fn listProblems(){ + print!("Problems: {}", problemNumbers[1]); + for problemNumber in 2..problemNumbers.len(){ + println!(", {}", problemNumbers[problemNumber]) + } + println!(); +} \ No newline at end of file diff --git a/src/Problems/Answer.rs b/src/Problems/Answer.rs index 6203a35..3f737e5 100644 --- a/src/Problems/Answer.rs +++ b/src/Problems/Answer.rs @@ -21,10 +21,13 @@ along with this program. If not, see . */ +#[allow(dead_code)] + pub struct Answer{ result: String, time: String, + nanoseconds: u128, } impl std::fmt::Display for Answer{ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result{ @@ -32,7 +35,16 @@ impl std::fmt::Display for Answer{ } } impl Answer{ - pub fn new(result: String, time: String) -> Answer{ - Answer{result, time} + pub fn new(result: String, time: String, nanoseconds: u128) -> Answer{ + Answer{result, time, nanoseconds} + } + pub fn getResult(&self) -> String{ + return format!("{}", self.result).to_string(); + } + pub fn getTime(&self) -> String{ + return format!("{}", self.time).to_string(); + } + pub fn getNano(&self) -> u128{ + return self.nanoseconds; } } diff --git a/src/Problems/Problem1.rs b/src/Problems/Problem1.rs index f688e86..1a7ad05 100644 --- a/src/Problems/Problem1.rs +++ b/src/Problems/Problem1.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems1.rs //Matthew Ellison // Created: 06-12-20 -//Modified: 06-13-20 +//Modified: 07-21-20 //What is the sum of all the multiples of 3 or 5 that are less than 1000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -47,10 +47,10 @@ pub fn solve() -> Answer{ timer.stop(); //Return the answer - return Answer::new("The sum of all numbers < 1000 is ".to_string() + &fullSum.to_string(), timer.getString()); + return Answer::new("The sum of all numbers < 1000 is ".to_string() + &fullSum.to_string(), timer.getString(), timer.getNano()); } /* Results: The sum of all numbers < 1000 is 233168 -It took 1.000 microseconds to solve this problem +It took an average of 893.000 nanoseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem10.rs b/src/Problems/Problem10.rs index efd54f1..31db888 100644 --- a/src/Problems/Problem10.rs +++ b/src/Problems/Problem10.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems10.rs //Matthew Ellison // Created: 06-15-20 -//Modified: 06-15-20 +//Modified: 07-21-20 //Find the sum of all the primes below two million //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -43,10 +43,10 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The sum of all the primes < {} is {}", GOAL_NUMBER, sum), timer.getString()); + return Answer::new(format!("The sum of all the primes < {} is {}", GOAL_NUMBER, sum), timer.getString(), timer.getNano()); } /* Results: The sum of all the primes < 2000000 is 142913828922 -It took 157.704 milliseconds to solve this problem +It took an average of 160.511 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem11.rs b/src/Problems/Problem11.rs index 5e64f5d..2377288 100644 --- a/src/Problems/Problem11.rs +++ b/src/Problems/Problem11.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems11.rs //Matthew Ellison // Created: 06-16-20 -//Modified: 06-16-20 +//Modified: 07-21-20 //What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid? /* 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 @@ -180,11 +180,11 @@ pub fn solve() -> Answer{ timer.stop(); //Return the resutls - return Answer::new(format!("The greatest product of 4 numbers in a line is {}\nThe numbers are {:?}", greatestProduct.iter().product::(), greatestProduct), timer.getString()); + return Answer::new(format!("The greatest product of 4 numbers in a line is {}\nThe numbers are {:?}", greatestProduct.iter().product::(), greatestProduct), timer.getString(), timer.getNano()); } /* Results: The greatest product of 4 numbers in a line is 70600674 The numbers are [89, 94, 97, 87] -It took 7.800 microseconds to solve this problem +It took an average of 7.478 microseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem12.rs b/src/Problems/Problem12.rs index 3955534..390dad3 100644 --- a/src/Problems/Problem12.rs +++ b/src/Problems/Problem12.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems12.rs //Matthew Ellison // Created: 06-16-20 -//Modified: 06-16-20 +//Modified: 07-21-20 //What is the value of the first triangle number to have over five hundred divisors? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -60,10 +60,10 @@ pub fn solve() -> Answer{ timer.stop(); //Return the solution - return Answer::new(format!("The triangular number {} is the sum of all numbers >= {} and has {} divisors", sum, counter - 1, divisors.len()), timer.getString()); + return Answer::new(format!("The triangular number {} is the sum of all numbers >= {} and has {} divisors", sum, counter - 1, divisors.len()), timer.getString(), timer.getNano()); } /* Results: The triangular number 76576500 is the sum of all numbers >= 12375 and has 576 divisors -It took 266.976 milliseconds to solve this problem +It took an average of 272.345 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem13.rs b/src/Problems/Problem13.rs index 271d6f3..ffff116 100644 --- a/src/Problems/Problem13.rs +++ b/src/Problems/Problem13.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems13.rs //Matthew Ellison // Created: 06-16-20 -//Modified: 06-16-20 +//Modified: 07-21-20 //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -146,11 +146,11 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The sum of all {} numbers is {}\nThe first 10 digits of the sum of the numbers is {}", nums.len(), sum, sum.to_str_radix(10).chars().take(10).collect::()), timer.getString()); + return Answer::new(format!("The sum of all {} numbers is {}\nThe first 10 digits of the sum of the numbers is {}", nums.len(), sum, sum.to_str_radix(10).chars().take(10).collect::()), timer.getString(), timer.getNano()); } /* Results: The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672 The first 10 digits of the sum of the numbers is 5537376230 -It took 34.600 microseconds to solve this problem +It took an average of 28.830 microseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem14.rs b/src/Problems/Problem14.rs index 36c3afa..2e24c3b 100644 --- a/src/Problems/Problem14.rs +++ b/src/Problems/Problem14.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems14.rs //Matthew Ellison // Created: 06-16-20 -//Modified: 06-16-20 +//Modified: 07-21-20 //Work out the first ten digits of the sum of the following one-hundred 50-digit numbers /* The following iterative sequence is defined for the set of positive integers: @@ -61,7 +61,7 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The number {} produced a chain of {} steps", maxNum, maxLength), timer.getString()); + return Answer::new(format!("The number {} produced a chain of {} steps", maxNum, maxLength), timer.getString(), timer.getNano()); } fn checkSeries(startNum: i64) -> i64{ @@ -86,5 +86,5 @@ fn checkSeries(startNum: i64) -> i64{ /* Results: The number 837799 produced a chain of 525 steps -It took 98.253 milliseconds to solve this problem +It took an average of 99.323 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem15.rs b/src/Problems/Problem15.rs index 7961d2c..08ce5da 100644 --- a/src/Problems/Problem15.rs +++ b/src/Problems/Problem15.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems15.rs //Matthew Ellison // Created: 06-16-20 -//Modified: 06-16-20 +//Modified: 07-21-20 //How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -51,7 +51,7 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The number of routes is {}", numOfRoutes), timer.getString()); + return Answer::new(format!("The number of routes is {}", numOfRoutes), timer.getString(), timer.getNano()); } //This function acts as a handler for moving the position on the grid and counting the distance @@ -75,5 +75,5 @@ fn moveLoc(currentX: i32, currentY: i32, numOfRoutes: &mut i64){ /* Results: The number of routes is 137846528820 -It took 11.804 minutes to solve this problem +It took an average of 12.199 minutes to run this problem through 10 iterations */ diff --git a/src/Problems/Problem16.rs b/src/Problems/Problem16.rs index 71e9e17..527f42a 100644 --- a/src/Problems/Problem16.rs +++ b/src/Problems/Problem16.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems16.rs //Matthew Ellison // Created: 06-16-20 -//Modified: 06-16-20 +//Modified: 07-21-20 //What is the sum of the digits of the number 2^1000? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -56,11 +56,11 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("{}^{} = {}\nThe sum of the elements is {}", NUM_TO_POWER, POWER, number, sumOfElements), timer.getString()); + return Answer::new(format!("{}^{} = {}\nThe sum of the elements is {}", NUM_TO_POWER, POWER, number, sumOfElements), timer.getString(), timer.getNano()); } /* Results: 2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 The sum of the elements is 1366 -It took 8.200 microseconds to solve this problem +It took an average of 6.483 microseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem17.rs b/src/Problems/Problem17.rs index 268eec8..4a44d79 100644 --- a/src/Problems/Problem17.rs +++ b/src/Problems/Problem17.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems17.rs //Matthew Ellison // Created: 06-16-20 -//Modified: 06-16-20 +//Modified: 07-21-20 //If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -49,7 +49,7 @@ pub fn solve() -> Answer{ } //Return the results - return Answer::new(format!("The sum of all the letters in all the number {}-{} is {}", START_NUM, STOP_NUM, sumOfLetters), timer.getString()); + return Answer::new(format!("The sum of all the letters in all the number {}-{} is {}", START_NUM, STOP_NUM, sumOfLetters), timer.getString(), timer.getNano()); } fn getStringFromNum(orgNumber: i32) -> String{ @@ -208,5 +208,5 @@ fn getNumberChars(number: String) -> i32{ /* Results: The sum of all the letters in all the number 1-1000 is 21124 -It took 356.800 microseconds to solve this problem +It took an average of 356.809 microseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem18.rs b/src/Problems/Problem18.rs index 1f4b4c9..549967a 100644 --- a/src/Problems/Problem18.rs +++ b/src/Problems/Problem18.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems18.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //Find the maximum total from top to bottom /* 75 @@ -135,7 +135,7 @@ pub fn solve() -> Answer{ let actualTotal = ((100 * NUM_ROWS) - foundPoints[foundPoints.len() - 1].total); //Return the results - return Answer::new(format!("The value of the longest path is {}", actualTotal), timer.getString()); + return Answer::new(format!("The value of the longest path is {}", actualTotal), timer.getString(), timer.getNano()); } //This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest fn invert(list: &mut Vec::>){ @@ -180,5 +180,5 @@ fn setupList(list: &mut Vec::>){ /* Results: The value of the longest path is 1074 -It took 19.400 microseconds to solve this problem +It took an average of 8.338 microseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem19.rs b/src/Problems/Problem19.rs index 15e8808..5b4bf30 100644 --- a/src/Problems/Problem19.rs +++ b/src/Problems/Problem19.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems19.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? /* You are given the following information, but you may prefer to do some research for yourself. @@ -74,7 +74,7 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("There are {} Sundays that landed on the first of the months from {} to {}", totalSundays, START_YEAR, END_YEAR), timer.getString()); + return Answer::new(format!("There are {} Sundays that landed on the first of the months from {} to {}", totalSundays, START_YEAR, END_YEAR), timer.getString(), timer.getNano()); } //Return the day of the week that the date you pass into it is on fn getDay(orgMonth: i32, orgDay: i32, orgYear: i32) -> DAYS{ @@ -193,5 +193,5 @@ fn isLeapYear(year: i32) -> bool{ /* Results: There are 171 Sundays that landed on the first of the months from 1901 to 2000 -It took 2.091 milliseconds to solve this problem +It took an average of 1.945 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem2.rs b/src/Problems/Problem2.rs index c357619..4c07552 100644 --- a/src/Problems/Problem2.rs +++ b/src/Problems/Problem2.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems2.rs //Matthew Ellison // Created: 06-12-20 -//Modified: 06-13-20 +//Modified: 07-21-20 //The sum of the even Fibonacci numbers less than 4,000,000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -45,10 +45,10 @@ pub fn solve() -> Answer{ timer.stop(); //Return the answer - return Answer::new("The sum of all even fibonacci numbers <= 3999999 is ".to_string() + &sum.to_string(), timer.getString()); + return Answer::new("The sum of all even fibonacci numbers <= 3999999 is ".to_string() + &sum.to_string(), timer.getString(), timer.getNano()); } /* Results: The sum of all even fibonacci numbers <= 3999999 is 4613732 -It took 5.100 microseconds to solve this problem +It took an average of 1.584 microseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem20.rs b/src/Problems/Problem20.rs index d5b37b3..5ae8812 100644 --- a/src/Problems/Problem20.rs +++ b/src/Problems/Problem20.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems20.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //What is the sum of the digits of 100!? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -55,11 +55,11 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("{}! = {}\nThe sum of the digits is: {}", TOP_NUM, number, sum), timer.getString()); + return Answer::new(format!("{}! = {}\nThe sum of the digits is: {}", TOP_NUM, number, sum), timer.getString(), timer.getNano()); } /* Results: 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 The sum of the digits is: 648 -It took 18.000 microseconds to solve this problem +It took an average of 15.565 microseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem21.rs b/src/Problems/Problem21.rs index 3bf6130..164ffb6 100644 --- a/src/Problems/Problem21.rs +++ b/src/Problems/Problem21.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems21.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //Evaluate the sum of all the amicable numbers under 10000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -81,7 +81,7 @@ pub fn solve() -> Answer{ answerString = format!("{}{}\n", answerString, amicable[cnt]); } answerString = format!("{}The sum of all of these amicable numbers is {}", answerString, amicable.iter().sum::()); - return Answer::new(answerString, timer.getString()); + return Answer::new(answerString, timer.getString(), timer.getNano()); } /* Results: @@ -97,5 +97,5 @@ All amicable numbers less than 10000 are 6232 6368 The sum of all of these amicable numbers is 31626 -It took 5.792 milliseconds to solve this problem +It took an average of 5.920 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem22.rs b/src/Problems/Problem22.rs index fd82136..fb5bd3f 100644 --- a/src/Problems/Problem22.rs +++ b/src/Problems/Problem22.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems22.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //What is the total of all the name scores in this file? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -433,10 +433,10 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The answer to the question is {}", sum), timer.getString()); + return Answer::new(format!("The answer to the question is {}", sum), timer.getString(), timer.getNano()); } /* Results: The answer to the question is 871198282 -It took 612.000 microseconds to solve this problem +It took an average of 613.952 microseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem23.rs b/src/Problems/Problem23.rs index b9026ec..7e9c3e4 100644 --- a/src/Problems/Problem23.rs +++ b/src/Problems/Problem23.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems23.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -72,7 +72,7 @@ pub fn solve() -> Answer{ timer.stop(); //Return the result - return Answer::new(format!("The answer is {}", sum), timer.getString()); + return Answer::new(format!("The answer is {}", sum), timer.getString(), timer.getNano()); } fn isSum(abund: &Vec, num: i64) -> bool{ @@ -95,5 +95,5 @@ fn isSum(abund: &Vec, num: i64) -> bool{ /* Results: The answer is 4179871 -It took 4.902 seconds to solve this problem +It took an average of 4.845 seconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem24.rs b/src/Problems/Problem24.rs index fe2bef7..60c6c1a 100644 --- a/src/Problems/Problem24.rs +++ b/src/Problems/Problem24.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems24.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -45,10 +45,10 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The 1 millionth permutation is {}", permutations[NEEDED_PERM - 1]), timer.getString()); + return Answer::new(format!("The 1 millionth permutation is {}", permutations[NEEDED_PERM - 1]), timer.getString(), timer.getNano()); } /* Results: The 1 millionth permutation is 2783915460 -It took 13.659 seconds to solve this problem +It took an average of 14.048 seconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem25.rs b/src/Problems/Problem25.rs index 542faca..0b8008c 100644 --- a/src/Problems/Problem25.rs +++ b/src/Problems/Problem25.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems25.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //What is the index of the first term in the Fibonacci sequence to contain 1000 digits? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -51,11 +51,11 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The first Fibonacci number with {} digits is {}\nIts index is {}", NUM_DIGITS, number, index), timer.getString()); + return Answer::new(format!("The first Fibonacci number with {} digits is {}\nIts index is {}", NUM_DIGITS, number, index), timer.getString(), timer.getNano()); } /* Results: The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816 Its index is 4782 -It took 1.681 seconds to solve this problem +It took an average of 1.819 seconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem26.rs b/src/Problems/Problem26.rs index 51e4ed0..2bbe02d 100644 --- a/src/Problems/Problem26.rs +++ b/src/Problems/Problem26.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems26.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-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 /* @@ -82,11 +82,11 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The longest cycle is {} digits long\nIt started with the number {}", longestCycle, longestNumber), timer.getString()); + return Answer::new(format!("The longest cycle is {} digits long\nIt started with the number {}", longestCycle, longestNumber), timer.getString(), timer.getNano()); } /* Results: The longest cycle is 982 digits long It started with the number 983 -It took 6.518 milliseconds to solve this problem +It took an average of 7.834 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem27.rs b/src/Problems/Problem27.rs index f7f3b4b..384bb3f 100644 --- a/src/Problems/Problem27.rs +++ b/src/Problems/Problem27.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems27.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -65,12 +65,12 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The greatest number of primes found is {}\nIt was found with A = {}, B = {}\nTHe product of A and B is {}", topN, topA, topB, topA * topB), timer.getString()); + return Answer::new(format!("The greatest number of primes found is {}\nIt was found with A = {}, B = {}\nTHe product of A and B is {}", topN, topA, topB, topA * topB), timer.getString(), timer.getNano()); } /* Results: The greatest number of primes found is 70 It was found with A = -61, B = 971 THe product of A and B is -59231 -It took 2.100 seconds to solve this problem +It took an average of 1.840 seconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem28.rs b/src/Problems/Problem28.rs index e367b83..353e9e9 100644 --- a/src/Problems/Problem28.rs +++ b/src/Problems/Problem28.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems28.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -43,7 +43,7 @@ pub fn solve() -> Answer{ timer.stop(); //Return the answer - return Answer::new(format!("The sum of the diagonals in the given grid is {}", sumOfDiagonals), timer.getString()); + return Answer::new(format!("The sum of the diagonals in the given grid is {}", sumOfDiagonals), timer.getString(), timer.getNano()); } fn setupGrid() -> Vec::>{ let mut grid = Vec::>::new(); @@ -133,5 +133,5 @@ fn findSum(grid: Vec::>) -> i64{ /* Results: The sum of the diagonals in the given grid is 669171001 -It took 7.651 milliseconds to solve this problem +It took an average of 7.760 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem29.rs b/src/Problems/Problem29.rs index cf1bc02..0c537af 100644 --- a/src/Problems/Problem29.rs +++ b/src/Problems/Problem29.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems29.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -58,10 +58,10 @@ pub fn solve() -> Answer{ timer.stop(); //Return the restuls - return Answer::new(format!("The number of unique values generated by a^b for {} <= a <= {} and {} <= b <= {} is {}", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.len()), timer.getString()); + return Answer::new(format!("The number of unique values generated by a^b for {} <= a <= {} and {} <= b <= {} is {}", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.len()), timer.getString(), timer.getNano()); } /* Result: The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183 -It took 107.204 milliseconds to solve this problem +It took an average of 96.917 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem3.rs b/src/Problems/Problem3.rs index 7fd124d..c146b6e 100644 --- a/src/Problems/Problem3.rs +++ b/src/Problems/Problem3.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems3.rs //Matthew Ellison // Created: 06-13-20 -//Modified: 06-13-20 +//Modified: 07-21-20 //The largest prime factor of 600851475143 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -47,10 +47,10 @@ pub fn solve() -> Answer{ timer.stop(); //Save the results - return Answer::new(format!("The largest factor of the number {} is {}", number, factors.last().unwrap()), timer.getString()); + return Answer::new(format!("The largest factor of the number {} is {}", number, factors.last().unwrap()), timer.getString(), timer.getNano()); } /* Results: The largest factor of the number 600851475143 is 6857 -It took 1.254 seconds to solve this problem +It took an average of 1.272 seconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem30.rs b/src/Problems/Problem30.rs index 8e2523d..e789f81 100644 --- a/src/Problems/Problem30.rs +++ b/src/Problems/Problem30.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems30.rs //Matthew Ellison // Created: 06-17-20 -//Modified: 06-17-20 +//Modified: 07-21-20 //Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -59,7 +59,7 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is {}", sumOfFifthNumbers.iter().sum::()), timer.getString()); + return Answer::new(format!("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is {}", sumOfFifthNumbers.iter().sum::()), timer.getString(), timer.getNano()); } //Returns a vector with the individual digits of the number passed to it fn getDigits(num: i64) -> Vec::{ @@ -76,5 +76,5 @@ fn getDigits(num: i64) -> Vec::{ /* Results: The sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839 -It took 320.693 milliseconds to solve this problem +It took an average of 319.230 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem31.rs b/src/Problems/Problem31.rs index deacde8..d040d24 100644 --- a/src/Problems/Problem31.rs +++ b/src/Problems/Problem31.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems31.rs //Matthew Ellison // Created: 06-19-20 -//Modified: 06-19-20 +//Modified: 07-21-20 //How many different ways can £2 be made using any number of coins? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -57,10 +57,10 @@ pub fn solve() -> Answer{ //Stop the timer timer.stop(); - return Answer::new(format!("There are {} ways to make 2 pounds with the given denominations of coins", permutations), timer.getString()); + return Answer::new(format!("There are {} ways to make 2 pounds with the given denominations of coins", permutations), timer.getString(), timer.getNano()); } /* Results: There are 73682 ways to make 2 pounds with the given denominations of coins -It took 109.300 microseconds to solve this problem +It took an average of 100.789 microseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem4.rs b/src/Problems/Problem4.rs index 8bccecb..8c82424 100644 --- a/src/Problems/Problem4.rs +++ b/src/Problems/Problem4.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems4.rs //Matthew Ellison // Created: 06-14-20 -//Modified: 06-14-20 +//Modified: 07-21-20 //Find the largest palindrome made from the product of two 3-digit numbers //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -65,10 +65,10 @@ pub fn solve() -> Answer{ palindromes.sort(); //Return the results - return Answer::new(format!("The largest palindrome is {}", palindromes[palindromes.len() - 1]), timer.getString()); + return Answer::new(format!("The largest palindrome is {}", palindromes[palindromes.len() - 1]), timer.getString(), timer.getNano()); } /* Results: The largest palindrome is 906609 -It took 109.264 milliseconds to solve this problem +It took an average of 114.371 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem5.rs b/src/Problems/Problem5.rs index e22ec2b..620a8c5 100644 --- a/src/Problems/Problem5.rs +++ b/src/Problems/Problem5.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems5.rs //Matthew Ellison // Created: 06-15-20 -//Modified: 06-15-20 +//Modified: 07-21-20 //What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -59,10 +59,10 @@ pub fn solve() -> Answer{ timer.stop(); //Save the results - return Answer::new(format!("The smallest positive number evenly divisibly by all number 1-20 is {}", currentNum), timer.getString()); + return Answer::new(format!("The smallest positive number evenly divisibly by all number 1-20 is {}", currentNum), timer.getString(), timer.getNano()); } /* Results: The smallest positive number evenly divisibly by all number 1-20 is 232792560 -It took 2.524 seconds to solve this problem +It took an average of 2.535 seconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem6.rs b/src/Problems/Problem6.rs index 874ffbc..b704bca 100644 --- a/src/Problems/Problem6.rs +++ b/src/Problems/Problem6.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems6.rs //Matthew Ellison // Created: 06-15-20 -//Modified: 06-15-20 +//Modified: 07-21-20 //Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -55,10 +55,10 @@ pub fn solve() -> Answer{ timer.stop(); //Save the results - return Answer::new(format!("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is {}\n", (sumOfSquares - squareOfSum).abs()), timer.getString()); + return Answer::new(format!("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is {}", (sumOfSquares - squareOfSum).abs()), timer.getString(), timer.getNano()); } /* Results: The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 24174150 -It took 0.000 nanoseconds to solve this problem +It took an average of 50.000 nanoseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem67.rs b/src/Problems/Problem67.rs index 6cce6e8..dc20bba 100644 --- a/src/Problems/Problem67.rs +++ b/src/Problems/Problem67.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems67.rs //Matthew Ellison // Created: 06-18-20 -//Modified: 06-18-20 +//Modified: 07-21-20 //Find the maximum total from top to bottom /* 59 @@ -220,7 +220,7 @@ pub fn solve() -> Answer{ let actualTotal = ((100 * NUM_ROWS) - foundPoints[foundPoints.len() - 1].total); //Return the results - return Answer::new(format!("The value of the longest path is {}", actualTotal), timer.getString()); + return Answer::new(format!("The value of the longest path is {}", actualTotal), timer.getString(), timer.getNano()); } //This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest fn invert(list: &mut Vec::>){ @@ -350,5 +350,5 @@ fn setupList(list: &mut Vec::>){ /* Results: The value of the longest path is 7273 -It took 61.174 milliseconds to solve this problem +It took an average of 61.385 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem7.rs b/src/Problems/Problem7.rs index 90885f8..91297bc 100644 --- a/src/Problems/Problem7.rs +++ b/src/Problems/Problem7.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems7.rs //Matthew Ellison // Created: 06-15-20 -//Modified: 06-15-20 +//Modified: 07-21-20 //What is the 10001th prime number? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -43,10 +43,10 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The {}th prime number is {}", NUMBER_OF_PRIMES, primes[primes.len() - 1]), timer.getString()); + return Answer::new(format!("The {}th prime number is {}", NUMBER_OF_PRIMES, primes[primes.len() - 1]), timer.getString(), timer.getNano()); } /* Results: The 10001th prime number is 104743 -It took 101.113 milliseconds to solve this problem +It took an average of 101.440 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem8.rs b/src/Problems/Problem8.rs index f57312d..e19c15a 100644 --- a/src/Problems/Problem8.rs +++ b/src/Problems/Problem8.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems8.rs //Matthew Ellison // Created: 06-15-20 -//Modified: 06-15-20 +//Modified: 07-21-20 //Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? /* 73167176531330624919225119674426574742355349194934 @@ -76,11 +76,11 @@ pub fn solve() -> Answer{ timer.stop(); //Return the results - return Answer::new(format!("The greatest product is {}\nThe numbers are {}", maxProduct, maxNums), timer.getString()); + return Answer::new(format!("The greatest product is {}\nThe numbers are {}", maxProduct, maxNums), timer.getString(), timer.getNano()); } /* Results: The greatest product is 23514624000 The numbers are 5576689664895 -It took 2.778 milliseconds to solve this problem +It took an average of 3.162 milliseconds to run this problem through 100 iterations */ diff --git a/src/Problems/Problem9.rs b/src/Problems/Problem9.rs index 6be0d45..30571fd 100644 --- a/src/Problems/Problem9.rs +++ b/src/Problems/Problem9.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems/Problems9.rs //Matthew Ellison // Created: 06-15-20 -//Modified: 06-15-20 +//Modified: 07-21-20 //There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -65,15 +65,15 @@ pub fn solve() -> Answer{ //Save the results if(found){ - return Answer::new(format!("The Pythagorean triplet is {} + {} + {}\nThe numbers' product is {}", a, b, c, a * b * c), timer.getString()); + return Answer::new(format!("The Pythagorean triplet is {} + {} + {}\nThe numbers' product is {}", a, b, c, a * b * c), timer.getString(), timer.getNano()); } else{ - return Answer::new(format!("The number was not found!"), timer.getString()); + return Answer::new(format!("The number was not found!"), timer.getString(), timer.getNano()); } } /* Result: The Pythagorean triplet is 200 + 375 + 425 The numbers' product is 31875000 -It took 141.900 microseconds to solve this problem +It took an average of 147.890 microseconds to run this problem through 100 iterations */ diff --git a/src/main.rs b/src/main.rs index 1f86165..5525a05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,15 +28,18 @@ mod Problems; +extern crate myClasses; + + +use std::io::Write; #[derive(PartialEq)] -enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} +enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, BENCHMARK, EXIT, SIZE} +#[allow(non_camel_case_types)] +#[derive(PartialEq)] +enum BenchmarkOptions{empty, runSpecific, runAllShort, runAll, exit, size} -static problemNumbers: [u32; 33] = [ 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, 67]; fn main(){ let mut selection = Selections::EMPTY; @@ -51,10 +54,12 @@ fn main(){ descriptionMenu(); } else if(selection == Selections::LIST){ - listProblems(); + Problems::listProblems(); + } + else if(selection == Selections::BENCHMARK){ + benchmarkMenu(); } else if(selection == Selections::EXIT){ - } else{ printErrorMessage(); @@ -66,6 +71,7 @@ fn printMenu(){ println!("{}. Solve a problem", Selections::SOLVE as i32); println!("{}. Print a problem description", Selections::DESCRIPTION as i32); println!("{}. List valid problem numbers", Selections::LIST as i32); + println!("{}. Benchmark", Selections::BENCHMARK as i32); println!("{}. Exit\n", Selections::EXIT as i32); } @@ -102,6 +108,9 @@ fn getSelection(selection: i32) -> Selections{ else if(selection == Selections::LIST as i32){ return Selections::LIST; } + else if(selection == Selections::BENCHMARK as i32){ + return Selections::BENCHMARK; + } else if(selection == Selections::EXIT as i32){ return Selections::EXIT; } @@ -113,296 +122,204 @@ fn printErrorMessage(){ println!("That is an invalid selection!"); } fn solveMenu(){ - let problemNumber = getProblemNumber(); + let problemNumber = Problems::getProblemNumber(); println!("\n\n"); //This selection solves all problems in order if(problemNumber == 0){ //Solve to every valid problem number - for problemLocation in 1..problemNumbers.len() as u32{ + for problemLocation in 1..Problems::problemNumbers.len() as u32{ //Solve the problems - print!("{}. ", problemNumbers[problemLocation as usize]); - solveProblem(problemNumbers[problemLocation as usize]); - println!("\n\n"); + print!("{}. ", Problems::problemNumbers[problemLocation as usize]); + let results = Problems::solveProblem(Problems::problemNumbers[problemLocation as usize], true, true); + println!("{}\n\n", results); } } //This is if a single problem number was chosen else{ //Solve the problem - solveProblem(problemNumber); + let results = Problems::solveProblem(problemNumber, true, true); + println!("{}", results); } println!("\n\n"); } -fn solveProblem(problemNumber: u32){ - if(problemNumber == 1){ - println!("{}", Problems::Problem1::getDescription()); - println!("{}", Problems::Problem1::solve()); - } - else if(problemNumber == 2){ - println!("{}", Problems::Problem2::getDescription()); - println!("{}", Problems::Problem2::solve()); - } - else if(problemNumber == 3){ - println!("{}", Problems::Problem3::getDescription()); - println!("{}", Problems::Problem3::solve()); - } - else if(problemNumber == 4){ - println!("{}", Problems::Problem4::getDescription()); - println!("{}", Problems::Problem4::solve()); - } - else if(problemNumber == 5){ - println!("{}", Problems::Problem5::getDescription()); - println!("{}", Problems::Problem5::solve()); - } - else if(problemNumber == 6){ - println!("{}", Problems::Problem6::getDescription()); - println!("{}", Problems::Problem6::solve()); - } - else if(problemNumber == 7){ - println!("{}", Problems::Problem7::getDescription()); - println!("{}", Problems::Problem7::solve()); - } - else if(problemNumber == 8){ - println!("{}", Problems::Problem8::getDescription()); - println!("{}", Problems::Problem8::solve()); - } - else if(problemNumber == 9){ - println!("{}", Problems::Problem9::getDescription()); - println!("{}", Problems::Problem9::solve()); - } - else if(problemNumber == 10){ - println!("{}", Problems::Problem10::getDescription()); - println!("{}", Problems::Problem10::solve()); - } - else if(problemNumber == 11){ - println!("{}", Problems::Problem11::getDescription()); - println!("{}", Problems::Problem11::solve()); - } - else if(problemNumber == 12){ - println!("{}", Problems::Problem12::getDescription()); - println!("{}", Problems::Problem12::solve()); - } - else if(problemNumber == 13){ - println!("{}", Problems::Problem13::getDescription()); - println!("{}", Problems::Problem13::solve()); - } - else if(problemNumber == 14){ - println!("{}", Problems::Problem14::getDescription()); - println!("{}", Problems::Problem14::solve()); - } - else if(problemNumber == 15){ - println!("{}", Problems::Problem15::getDescription()); - println!("{}", Problems::Problem15::solve()); - } - else if(problemNumber == 16){ - println!("{}", Problems::Problem16::getDescription()); - println!("{}", Problems::Problem16::solve()); - } - else if(problemNumber == 17){ - println!("{}", Problems::Problem17::getDescription()); - println!("{}", Problems::Problem17::solve()); - } - else if(problemNumber == 18){ - println!("{}", Problems::Problem18::getDescription()); - println!("{}", Problems::Problem18::solve()); - } - else if(problemNumber == 19){ - println!("{}", Problems::Problem19::getDescription()); - println!("{}", Problems::Problem19::solve()); - } - else if(problemNumber == 20){ - println!("{}", Problems::Problem20::getDescription()); - println!("{}", Problems::Problem20::solve()); - } - else if(problemNumber == 21){ - println!("{}", Problems::Problem21::getDescription()); - println!("{}", Problems::Problem21::solve()); - } - else if(problemNumber == 22){ - println!("{}", Problems::Problem22::getDescription()); - println!("{}", Problems::Problem22::solve()); - } - else if(problemNumber == 23){ - println!("{}", Problems::Problem23::getDescription()); - println!("{}", Problems::Problem23::solve()); - } - else if(problemNumber == 24){ - println!("{}", Problems::Problem24::getDescription()); - println!("{}", Problems::Problem24::solve()); - } - else if(problemNumber == 25){ - println!("{}", Problems::Problem25::getDescription()); - println!("{}", Problems::Problem25::solve()); - } - else if(problemNumber == 26){ - println!("{}", Problems::Problem26::getDescription()); - println!("{}", Problems::Problem26::solve()); - } - else if(problemNumber == 27){ - println!("{}", Problems::Problem27::getDescription()); - println!("{}", Problems::Problem27::solve()); - } - else if(problemNumber == 28){ - println!("{}", Problems::Problem28::getDescription()); - println!("{}", Problems::Problem28::solve()); - } - else if(problemNumber == 29){ - println!("{}", Problems::Problem29::getDescription()); - println!("{}", Problems::Problem29::solve()); - } - else if(problemNumber == 30){ - println!("{}", Problems::Problem30::getDescription()); - println!("{}", Problems::Problem30::solve()); - } - else if(problemNumber == 31){ - println!("{}", Problems::Problem31::getDescription()); - println!("{}", Problems::Problem31::solve()); - } - else if(problemNumber == 67){ - println!("{}", Problems::Problem67::getDescription()); - println!("{}", Problems::Problem67::solve()); - } -} fn descriptionMenu(){ //Give some extra space to print the description println!("\n\n"); //Get the problem number - let problemNumber = getProblemNumber(); + let problemNumber = Problems::getProblemNumber(); + let mut results = Problems::Answer::Answer::new("".to_string(), "".to_string(), 0); //If the problem number is 0 then print out all the descriptions if(problemNumber == 0){ //Print decription for every valid problem number - for problemLocation in 1..problemNumbers.len(){ + for problemLocation in 1..Problems::problemNumbers.len(){ //Print the problem's description - print!("{}. ", problemNumbers[problemLocation]); - printDescription(problemNumbers[problemLocation]); - println!(); + print!("{}. ", Problems::problemNumbers[problemLocation]); + results = Problems::solveProblem(Problems::problemNumbers[problemLocation], true, false); } + println!("{}", results); } //Otherwise print out a single problem's description else{ - printDescription(problemNumber); + results = Problems::solveProblem(problemNumber, true, false); + println!("{}", results); } println!("\n\n"); } -fn printDescription(problemNumber: u32){ - if(problemNumber == 1){ - println!("{}", Problems::Problem1::getDescription()); - } - else if(problemNumber == 2){ - println!("{}", Problems::Problem2::getDescription()); - } - else if(problemNumber == 3){ - println!("{}", Problems::Problem3::getDescription()); - } - else if(problemNumber == 4){ - println!("{}", Problems::Problem4::getDescription()); - } - else if(problemNumber == 5){ - println!("{}", Problems::Problem5::getDescription()); - } - else if(problemNumber == 6){ - println!("{}", Problems::Problem6::getDescription()); - } - else if(problemNumber == 7){ - println!("{}", Problems::Problem7::getDescription()); - } - else if(problemNumber == 8){ - println!("{}", Problems::Problem8::getDescription()); - } - else if(problemNumber == 9){ - println!("{}", Problems::Problem9::getDescription()); - } - else if(problemNumber == 10){ - println!("{}", Problems::Problem10::getDescription()); - } - else if(problemNumber == 11){ - println!("{}", Problems::Problem11::getDescription()); - } - else if(problemNumber == 12){ - println!("{}", Problems::Problem12::getDescription()); - } - else if(problemNumber == 13){ - println!("{}", Problems::Problem13::getDescription()); - } - else if(problemNumber == 14){ - println!("{}", Problems::Problem14::getDescription()); - } - else if(problemNumber == 15){ - println!("{}", Problems::Problem15::getDescription()); - } - else if(problemNumber == 16){ - println!("{}", Problems::Problem16::getDescription()); - } - else if(problemNumber == 17){ - println!("{}", Problems::Problem17::getDescription()); - } - else if(problemNumber == 18){ - println!("{}", Problems::Problem18::getDescription()); - } - else if(problemNumber == 19){ - println!("{}", Problems::Problem19::getDescription()); - } - else if(problemNumber == 20){ - println!("{}", Problems::Problem20::getDescription()); - } - else if(problemNumber == 21){ - println!("{}", Problems::Problem21::getDescription()); - } - else if(problemNumber == 22){ - println!("{}", Problems::Problem22::getDescription()); - } - else if(problemNumber == 23){ - println!("{}", Problems::Problem23::getDescription()); - } - else if(problemNumber == 24){ - println!("{}", Problems::Problem24::getDescription()); - } - else if(problemNumber == 25){ - println!("{}", Problems::Problem25::getDescription()); - } - else if(problemNumber == 26){ - println!("{}", Problems::Problem26::getDescription()); - } - else if(problemNumber == 27){ - println!("{}", Problems::Problem27::getDescription()); - } - else if(problemNumber == 28){ - println!("{}", Problems::Problem28::getDescription()); - } - else if(problemNumber == 29){ - println!("{}", Problems::Problem29::getDescription()); - } - else if(problemNumber == 30){ - println!("{}", Problems::Problem30::getDescription()); - } - else if(problemNumber == 31){ - println!("{}", Problems::Problem31::getDescription()); - } - else if(problemNumber == 67){ - println!("{}", Problems::Problem67::getDescription()); - } -} -fn getProblemNumber() -> u32{ - println!("Enter a problem number: "); - //Setup reading from the console - let mut problemNumberStr = String::new(); - std::io::stdin().read_line(&mut problemNumberStr).ok(); - //Convert the read to an int - let mut problemNumber = problemNumberStr.trim().parse::().unwrap(); +fn benchmarkMenu(){ + benchmarkPrintMenu(); + let selection = benchmarkGetMenuSelection(); - //Loop through all valid problem numbers and see if the one entered matches it - while(!problemNumbers.contains(&problemNumber)){ - println!("That is an invalid problem number!\nEnter a problem number: "); - std::io::stdin().read_line(&mut problemNumberStr).ok(); - problemNumber = problemNumberStr.trim().parse::().unwrap(); + if(selection == BenchmarkOptions::runSpecific){ + benchmarkRunSpecific(); } - return problemNumber; -} -fn listProblems(){ - print!("Problems: {}", problemNumbers[1]); - for problemNumber in 2..problemNumbers.len(){ - println!(", {}", problemNumbers[problemNumber]) + else if(selection == BenchmarkOptions::runAllShort){ + benchmarkRunAllShort(); + } + else if(selection == BenchmarkOptions::runAll){ + benchmarkRunAll(); + } + else if(selection == BenchmarkOptions::exit){ + } + else if(selection == BenchmarkOptions::empty){ + } + else{ + printErrorMessage(); } - println!(); +} +fn benchmarkPrintMenu(){ + println!("{}. Run a specific problem", BenchmarkOptions::runSpecific as i32); + println!("{}. Run all problems that have a reasonably short run time", BenchmarkOptions::runAllShort as i32); + println!("{}. Run all problems", BenchmarkOptions::runAll as i32); + println!("{}. Exit the menu\n", BenchmarkOptions::exit as i32); +} +fn benchmarkGetMenuSelection() -> BenchmarkOptions{ + //Setup reading from the console + let mut selectionStr = String::new(); + + std::io::stdin().read_line(&mut selectionStr).ok(); + //Convert the read to an int + let mut selection = selectionStr.trim().parse::().unwrap(); + while(!isValidMenu(selection)){ + println!("That is an invalid option!"); + printMenu(); + std::io::stdin().read_line(&mut selectionStr).ok(); + selection = selectionStr.trim().parse::().unwrap(); + } + return benchmarkGetSelection(selection); +} +fn benchmarkGetSelection(selection: i32) -> BenchmarkOptions{ + if(selection == BenchmarkOptions::runSpecific as i32){ + return BenchmarkOptions::runSpecific; + } + else if(selection == BenchmarkOptions::runAllShort as i32){ + return BenchmarkOptions::runAllShort; + } + else if(selection == BenchmarkOptions::runAll as i32){ + return BenchmarkOptions::runAll; + } + else if(selection == BenchmarkOptions::exit as i32){ + return BenchmarkOptions::exit; + } + else{ + return BenchmarkOptions::size; + } +} +fn benchmarkRunSpecific(){ + //Ask which problem the user wants to run + let problemNumber = Problems::getProblemNumber(); + //Ask how many times to run the problem + let timesToRun = getNumberOfTimesToRun(); + + //Print the problem's description + println!("{}. ", problemNumber); + Problems::solveProblem(problemNumber, true, false); + //Run the problem the specific number of times + let mut totalTime = 0; + let results = benchmarkSolveProblem(problemNumber, timesToRun, &mut totalTime); + + //Print the results + println!("{}", getBenchmarkResults(totalTime, timesToRun, results)); +} +fn benchmarkRunAllShort(){ + //Ask how many times to run the problem + let timesToRun = getNumberOfTimesToRun(); + + //Run all problems that are not too long + for problemLocation in 1..Problems::problemNumbers.len() as u32{ + //Skip any problem that is too long + let problemNumber = Problems::problemNumbers[problemLocation as usize]; + if(Problems::tooLong.contains(&problemNumber)){ + continue; + } + //Solve the problems + println!("{}. ", problemNumber); + Problems::solveProblem(problemNumber, true, false); + let mut totalTime = 0; + let results = benchmarkSolveProblem(problemNumber, timesToRun, &mut totalTime); + println!("\n\n"); + + //Print the results + println!("{}", getBenchmarkResults(totalTime, timesToRun, results)); + } +} +fn benchmarkRunAll(){ + //Ask how many times to run the problem + let timesToRun = getNumberOfTimesToRun(); + + //Run all problems + for problemLocation in 1..Problems::problemNumbers.len() as u32{ + //Get the problem number + let problemNumber = Problems::problemNumbers[problemLocation as usize]; + //Solve the problems + println!("{}. ", problemNumber); + Problems::solveProblem(problemNumber, true, false); + let mut totalTime = 0; + let results = benchmarkSolveProblem(problemNumber, timesToRun, &mut totalTime); + println!("\n\n"); + + //Print the results + println!("{}", getBenchmarkResults(totalTime, timesToRun, results)); + } +} +fn getNumberOfTimesToRun() -> u32{ + print!("How many times do you want to run this problem? "); + std::io::stdout().flush().unwrap(); + let mut numOfTimesToRunString = String::new(); + std::io::stdin().read_line(&mut numOfTimesToRunString).ok(); + //Convert to an int + let mut numOfTimesToRun = numOfTimesToRunString.trim().parse::().unwrap(); + + while(numOfTimesToRun < 1){ + println!("That is an invalid number!"); + println!("How many times to you want to run this problem? "); + std::io::stdin().read_line(&mut numOfTimesToRunString).ok(); + //Convert to an int + numOfTimesToRun = numOfTimesToRunString.trim().parse::().unwrap(); + } + return numOfTimesToRun; +} +fn benchmarkSolveProblem(problemNumber: u32, timesToRun: u32, totalTime: &mut u128) -> String{ + let mut answer = Problems::Answer::Answer::new("".to_string(), "".to_string(), 0); + *totalTime = 0; + print!("Solving"); + for _ in 0..timesToRun{ + print!("."); + std::io::stdout().flush().unwrap(); + //Solve the problem + answer = Problems::solveProblem(problemNumber, false, true); + //Get the time data + *totalTime += &answer.getNano(); + } + let results = format!("{}", answer.getResult()); + return results.to_string(); +} +fn getBenchmarkResults(totalTime: u128, timesRun: u32, results: String) -> String{ + //Calculate the average run time of the problem + let averageTime = (totalTime as f64) / (timesRun as f64); + let timeResults = myClasses::Stopwatch::Stopwatch::getStr(averageTime); + //Tally the results + let benchmarkResult = format!("\n\n{}\nIt took an average of {} to run this problem through {} iterations\n\n", results, timeResults, timesRun); + return benchmarkResult; }