diff --git a/src/Problems.rs b/src/Problems.rs index 3eb40e9..cf5b909 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -43,3 +43,4 @@ pub mod Problem17; pub mod Problem18; pub mod Problem19; pub mod Problem20; +pub mod Problem21; diff --git a/src/Problems/Problem21.rs b/src/Problems/Problem21.rs new file mode 100644 index 0000000..3bf6130 --- /dev/null +++ b/src/Problems/Problem21.rs @@ -0,0 +1,101 @@ +//ProjectEulerRust/src/Problems/Problems21.rs +//Matthew Ellison +// Created: 06-17-20 +//Modified: 06-17-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 +/* + 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{ + "Evaluate the sum of all the amicable numbers under 10000".to_string() +} + +pub fn solve() -> Answer{ + let LIMIT = 10000; + //Setup the variables + let mut divisorSum = Vec::::new(); + //Make sure the array is filled with 0's + while(divisorSum.len() < LIMIT){ + divisorSum.push(0); + } + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Generate the divisors of all numbers < 10000, get their sum, and add it to the list + for cnt in 1..LIMIT{ + let mut divisors = myClasses::Algorithms::getDivisors(cnt as i64); + if(divisors.len() > 1){ + divisors.remove(divisors.len() - 1); + } + divisorSum[cnt] = divisors.iter().sum(); + } + //Check every sum of divisors in the list for matching sum + let mut amicable = Vec::::new(); + for cnt in 1..divisorSum.len(){ + let sum = divisorSum[cnt]; + //If the sum is greater than the number of divisors then it is impossible to be amicable. Skip the number and continue + if(sum >= divisorSum.len() as i64){ + continue; + } + //We know that divisorSum.at(cnt) == sum, so if divisorSum.at(sum) == cnt we found an amicable number + if(divisorSum[sum as usize] == cnt as i64){ + //A number can't be amicable with itself + if(sum == cnt as i64){ + continue; + } + //Add it to the array of amicable numbers + amicable.push(cnt as i64); + } + } + + //Stop the timer + timer.stop(); + + //Sort the array for neatness + amicable.sort(); + + //Return the results + let mut answerString = format!("All amicable numbers less than {} are\n", LIMIT); + for cnt in 0..amicable.len(){ + 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()); +} + +/* Results: +All amicable numbers less than 10000 are +220 +284 +1184 +1210 +2620 +2924 +5020 +5564 +6232 +6368 +The sum of all of these amicable numbers is 31626 +It took 5.792 milliseconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index ef2dcb3..89e4093 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/main.rs //Matthew Ellison // Created: 06-11-20 -//Modified: 06-15-20 +//Modified: 06-17-20 //This is a driver function for the Project Euler solutions in Rust //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses /* @@ -33,9 +33,9 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 21] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +static problemNumbers: [u32; 22] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20]; + 20, 21]; fn main(){ let mut selection = Selections::EMPTY; @@ -212,6 +212,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem20::getDescription()); println!("{}", Problems::Problem20::solve()); } + else if(problemNumber == 21){ + println!("{}", Problems::Problem21::getDescription()); + println!("{}", Problems::Problem21::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -297,6 +301,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 20){ println!("{}", Problems::Problem20::getDescription()); } + else if(problemNumber == 21){ + println!("{}", Problems::Problem21::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");