diff --git a/src/Problems.rs b/src/Problems.rs index 5c5151b..f445fe7 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -31,3 +31,4 @@ pub mod Problem5; pub mod Problem6; pub mod Problem7; pub mod Problem8; +pub mod Problem9; diff --git a/src/Problems/Problem9.rs b/src/Problems/Problem9.rs new file mode 100644 index 0000000..6be0d45 --- /dev/null +++ b/src/Problems/Problem9.rs @@ -0,0 +1,79 @@ +//ProjectEulerRust/src/Problems/Problems9.rs +//Matthew Ellison +// Created: 06-15-20 +//Modified: 06-15-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 +/* + 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 . +*/ + + +extern crate myClasses; +use crate::Problems::Answer::Answer; + + +pub fn getDescription() -> String{ + "There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.".to_string() +} + +pub fn solve() -> Answer{ + let mut a = 1.0; //Holds the length of the first side + let mut b = 0.0; //Holds the length of the second side + let mut c = 0.0;//Holds the length of the hyp + let mut found = false; //A flag to show whether the solution has been found + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Loop through all possible a's + while((a < 1000.0) && !found){ + b = a + 1.0; //b must be larget than a + c = (((a * a) + (b * b)) as f64).sqrt(); + + //Loop through all possible b's for this a + while((a + b + c) < 1000.0){ + b += 1.0; + c = (((a * a) + (b * b)) as f64).sqrt(); + } + + //If the sum is 1000 you found the number, otherwise go to the next possible a + if(a + b + c == 1000.0){ + found = true; + } + else{ + a += 1.0; + } + } + + //Stop the timer + timer.stop(); + + //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()); + } + else{ + return Answer::new(format!("The number was not found!"), timer.getString()); + } +} + +/* Result: +The Pythagorean triplet is 200 + 375 + 425 +The numbers' product is 31875000 +It took 141.900 microseconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index 7e15bac..fdc6c34 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 9] = [0, 1, 2, 3, 4, 5, 6, 7, 8]; +static problemNumbers: [u32; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; fn main(){ let mut selection = Selections::EMPTY; @@ -162,6 +162,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem8::getDescription()); println!("{}", Problems::Problem8::solve()); } + else if(problemNumber == 9){ + println!("{}", Problems::Problem9::getDescription()); + println!("{}", Problems::Problem9::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -211,6 +215,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 8){ println!("{}", Problems::Problem8::getDescription()); } + else if(problemNumber == 9){ + println!("{}", Problems::Problem9::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");