Added solution to problem 4

This commit is contained in:
2020-06-14 18:44:12 -04:00
parent 9c16d61a09
commit e9ffc487ae
3 changed files with 78 additions and 1 deletions

View File

@@ -9,3 +9,4 @@ pub mod Answer;
pub mod Problem1;
pub mod Problem2;
pub mod Problem3;
pub mod Problem4;

69
src/Problems/Problem4.rs Normal file
View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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::<i64>::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());
}

View File

@@ -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: ");