From 16365a5dff3abe3f2e21dcbfeb480b3ddfc46fff Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 17 Jun 2020 14:54:21 -0400 Subject: [PATCH] Added solution to problem 23 --- src/Problems.rs | 1 + src/Problems/Problem23.rs | 99 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 11 ++++- 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/Problems/Problem23.rs diff --git a/src/Problems.rs b/src/Problems.rs index f22438e..cb22dea 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -45,3 +45,4 @@ pub mod Problem19; pub mod Problem20; pub mod Problem21; pub mod Problem22; +pub mod Problem23; diff --git a/src/Problems/Problem23.rs b/src/Problems/Problem23.rs new file mode 100644 index 0000000..052f2f9 --- /dev/null +++ b/src/Problems/Problem23.rs @@ -0,0 +1,99 @@ +//ProjectEulerRust/src/Problems/Problems22.rs +//Matthew Ellison +// Created: 06-17-20 +//Modified: 06-17-20 +//What is the total of all the name scores in this file? +//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 total of all the name scores in this file?".to_string() +} + +pub fn solve() -> Answer{ + let MAX_NUM = 28123; + //Setup the variables + let mut divisorSums = Vec::::new(); //Holds the sum of all the divisors of a number + //Make sure every element has a 0 in it's location + for _ in 0..=MAX_NUM{ + divisorSums.push(0); + } + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Get the sum of the divisors of all numbers < MAX_NUM + for cnt in 1..MAX_NUM{ + let mut div = myClasses::Algorithms::getDivisors(cnt); + //Remove the last element, which is the number itself. This gives us the propper divisors + if(div.len() > 1){ + div.remove(div.len() - 1); + } + divisorSums[cnt as usize] = div.iter().sum(); + } + + //Get the abundant numbers + let mut abund = Vec::::new(); + for cnt in 0..divisorSums.len(){ + if(divisorSums[cnt] > cnt as i64){ + abund.push(cnt as i64); + } + } + + //Check if each number can be the sum of 2 abundant numbers and add to the sum if no + let mut sum = 0; + for cnt in 1..MAX_NUM{ + if(!isSum(&abund, cnt)){ + sum += cnt; + } + } + + //Stop the timer + timer.stop(); + + //Return the result + return Answer::new(format!("The answer is {}", sum), timer.getString()); +} + +fn isSum(abund: &Vec, num: i64) -> bool{ + //Pick a number for the first part of the sum + for firstNum in 0..abund.len(){ + //Pick a number for the second part of the sum + for secondNum in firstNum..abund.len(){ + let sum = abund[firstNum] + abund[secondNum]; + if(sum == num){ + return true; + } + else if(sum > num){ + break; + } + } + } + //If you have run through the entire list and did not find a sum then it is false + return false; +} + +/* Results: +The answer is 4179871 +It took 4.902 seconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index 4755789..35249a5 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; 23] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +static problemNumbers: [u32; 24] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22]; + 20, 21, 22, 23]; fn main(){ let mut selection = Selections::EMPTY; @@ -220,6 +220,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem22::getDescription()); println!("{}", Problems::Problem22::solve()); } + else if(problemNumber == 23){ + println!("{}", Problems::Problem23::getDescription()); + println!("{}", Problems::Problem23::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -311,6 +315,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 22){ println!("{}", Problems::Problem22::getDescription()); } + else if(problemNumber == 23){ + println!("{}", Problems::Problem23::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");