From f654a7d172b0daa16d67d8bb1118f5f1dcd0ce7a Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 16 Jun 2020 11:32:38 -0400 Subject: [PATCH] Added solution to problem 12 --- src/Problems.rs | 1 + src/Problems/Problem12.rs | 69 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 ++++- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/Problems/Problem12.rs diff --git a/src/Problems.rs b/src/Problems.rs index 30d81da..1acad00 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -34,3 +34,4 @@ pub mod Problem8; pub mod Problem9; pub mod Problem10; pub mod Problem11; +pub mod Problem12; diff --git a/src/Problems/Problem12.rs b/src/Problems/Problem12.rs new file mode 100644 index 0000000..3955534 --- /dev/null +++ b/src/Problems/Problem12.rs @@ -0,0 +1,69 @@ +//ProjectEulerRust/src/Problems/Problems12.rs +//Matthew Ellison +// Created: 06-16-20 +//Modified: 06-16-20 +//What is the value of the first triangle number to have over five hundred divisors? +//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 value of the first triangle number to have over five hundred divisors?".to_string() +} + +pub fn solve() -> Answer{ + //Setup the other variables + let GOAL_DIVISORS = 500i64; + let mut foundNumber = false; //To flag whether the number has been found + let mut sum = 1i64; + let mut counter = 2i64; //The next number to be added to the sum to make a triangular number + let mut divisors = Vec::::new(); + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Loop until you find the appropriate number + while((!foundNumber) && (sum > 0)){ + divisors = myClasses::Algorithms::getDivisors(sum); + //If the number of divisors is correct set the flag + if(divisors.len() as i64 > GOAL_DIVISORS){ + foundNumber = true; + } + //Otherwise add to the sum and increase the next number + else{ + sum += counter; + counter += 1; + } + } + + //Stop the timer + timer.stop(); + + //Return the solution + return Answer::new(format!("The triangular number {} is the sum of all numbers >= {} and has {} divisors", sum, counter - 1, divisors.len()), timer.getString()); +} + +/* Results: +The triangular number 76576500 is the sum of all numbers >= 12375 and has 576 divisors +It took 266.976 milliseconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index 9337d5e..418c752 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 12] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; +static problemNumbers: [u32; 13] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; fn main(){ let mut selection = Selections::EMPTY; @@ -174,6 +174,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem11::getDescription()); println!("{}", Problems::Problem11::solve()); } + else if(problemNumber == 12){ + println!("{}", Problems::Problem12::getDescription()); + println!("{}", Problems::Problem12::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -232,6 +236,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 11){ println!("{}", Problems::Problem11::getDescription()); } + else if(problemNumber == 12){ + println!("{}", Problems::Problem12::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");