From 34bdc3b98bd5c955362adf5209af01ac7f32f477 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 17 Jun 2020 17:39:16 -0400 Subject: [PATCH] Added solution to problem 24 --- src/Problems.rs | 1 + src/Problems/Problem23.rs | 6 ++--- src/Problems/Problem24.rs | 54 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 11 ++++++-- 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/Problems/Problem24.rs diff --git a/src/Problems.rs b/src/Problems.rs index cb22dea..c136a4e 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -46,3 +46,4 @@ pub mod Problem20; pub mod Problem21; pub mod Problem22; pub mod Problem23; +pub mod Problem24; diff --git a/src/Problems/Problem23.rs b/src/Problems/Problem23.rs index 052f2f9..b9026ec 100644 --- a/src/Problems/Problem23.rs +++ b/src/Problems/Problem23.rs @@ -1,8 +1,8 @@ -//ProjectEulerRust/src/Problems/Problems22.rs +//ProjectEulerRust/src/Problems/Problems23.rs //Matthew Ellison // Created: 06-17-20 //Modified: 06-17-20 -//What is the total of all the name scores in this file? +//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 /* Copyright (C) 2020 Matthew Ellison @@ -26,7 +26,7 @@ extern crate myClasses; use crate::Problems::Answer::Answer; pub fn getDescription() -> String{ - "What is the total of all the name scores in this file?".to_string() + "Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers".to_string() } pub fn solve() -> Answer{ diff --git a/src/Problems/Problem24.rs b/src/Problems/Problem24.rs new file mode 100644 index 0000000..fe2bef7 --- /dev/null +++ b/src/Problems/Problem24.rs @@ -0,0 +1,54 @@ +//ProjectEulerRust/src/Problems/Problems24.rs +//Matthew Ellison +// Created: 06-17-20 +//Modified: 06-17-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 +/* + 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{ + "What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?".to_string() +} + +pub fn solve() -> Answer{ + let NEEDED_PERM = 1_000_000; + //Setup the variables + let nums = "0123456789".to_string(); //The string that you are trying to find the permutations of + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Get all the permutations of the string + let permutations = myClasses::Algorithms::getPermutations(nums); + + //Stop the timer + timer.stop(); + + //Return the results + return Answer::new(format!("The 1 millionth permutation is {}", permutations[NEEDED_PERM - 1]), timer.getString()); +} + +/* Results: +The 1 millionth permutation is 2783915460 +It took 13.659 seconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index 35249a5..7e50c98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,9 +33,9 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 24] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +static problemNumbers: [u32; 25] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23]; + 20, 21, 22, 23, 24]; fn main(){ let mut selection = Selections::EMPTY; @@ -224,6 +224,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem23::getDescription()); println!("{}", Problems::Problem23::solve()); } + else if(problemNumber == 24){ + println!("{}", Problems::Problem24::getDescription()); + println!("{}", Problems::Problem24::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -318,6 +322,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 23){ println!("{}", Problems::Problem23::getDescription()); } + else if(problemNumber == 24){ + println!("{}", Problems::Problem24::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");