From e9ffc487ae9758d064ddaeb60d60643d39a20215 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 14 Jun 2020 18:44:12 -0400 Subject: [PATCH] Added solution to problem 4 --- src/Problems.rs | 1 + src/Problems/Problem4.rs | 69 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 +++++- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/Problems/Problem4.rs diff --git a/src/Problems.rs b/src/Problems.rs index 685288d..c39f4b1 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -9,3 +9,4 @@ pub mod Answer; pub mod Problem1; pub mod Problem2; pub mod Problem3; +pub mod Problem4; diff --git a/src/Problems/Problem4.rs b/src/Problems/Problem4.rs new file mode 100644 index 0000000..4fad4b8 --- /dev/null +++ b/src/Problems/Problem4.rs @@ -0,0 +1,69 @@ +//ProjectEulerRust/src/Problems/Problems4.rs +//Matthew Ellison +// Created: 06-14-20 +//Modified: 06-14-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/JavaClasses +/* + Copyright (C) 2019 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{ + "Find the largest palindrome made from the product of two 3-digit numbers".to_string() +} + +pub fn solve() -> Answer{ + let START_NUM = 100; + let END_NUM = 999; + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + let mut palindromes = Vec::::new(); + + //Start at the first 3-digit number and check every one up to the last 3-digit number + for firstNum in START_NUM..END_NUM{ + //You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100) + for secondNum in firstNum..END_NUM{ + //Get the product of the numbers + let product = firstNum * secondNum; + //Change the number into a string + let productString: String = product.to_string(); + //Reverse the string + let reverseString: String = productString.chars().rev().collect(); + + //If the number and it's reverse are the same it is a palindrome so add it to the list + if(productString == reverseString){ + palindromes.push(product); + } + //If it's not a palindrome ignore it and move to the next number + } + } + + //Stop the timer + timer.stop(); + + //Sort the palindromes so that the last one is the largest + palindromes.sort(); + + //Return the results + return Answer::new(format!("The largest palindrome is {}", palindromes[palindromes.len() - 1]), timer.getString()); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3489ccd..6a9ed89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 4] = [0, 1, 2, 3]; +static problemNumbers: [u32; 5] = [0, 1, 2, 3, 4]; fn main(){ let mut selection = Selections::EMPTY; @@ -125,6 +125,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem3::getDescription()); println!("{}", Problems::Problem3::solve()); } + else if(problemNumber == 4){ + println!("{}", Problems::Problem4::getDescription()); + println!("{}", Problems::Problem4::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -159,6 +163,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 3){ println!("{}", Problems::Problem3::getDescription()); } + else if(problemNumber == 4){ + println!("{}", Problems::Problem4::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");