Added solution to problem 12

This commit is contained in:
2020-06-16 11:32:38 -04:00
parent ef2948b988
commit f654a7d172
3 changed files with 78 additions and 1 deletions

View File

@@ -34,3 +34,4 @@ pub mod Problem8;
pub mod Problem9; pub mod Problem9;
pub mod Problem10; pub mod Problem10;
pub mod Problem11; pub mod Problem11;
pub mod Problem12;

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

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

View File

@@ -33,7 +33,7 @@ mod Problems;
#[derive(PartialEq)] #[derive(PartialEq)]
enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} 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(){ fn main(){
let mut selection = Selections::EMPTY; let mut selection = Selections::EMPTY;
@@ -174,6 +174,10 @@ fn solveProblem(problemNumber: u32){
println!("{}", Problems::Problem11::getDescription()); println!("{}", Problems::Problem11::getDescription());
println!("{}", Problems::Problem11::solve()); println!("{}", Problems::Problem11::solve());
} }
else if(problemNumber == 12){
println!("{}", Problems::Problem12::getDescription());
println!("{}", Problems::Problem12::solve());
}
} }
fn descriptionMenu(){ fn descriptionMenu(){
//Give some extra space to print the description //Give some extra space to print the description
@@ -232,6 +236,9 @@ fn printDescription(problemNumber: u32){
else if(problemNumber == 11){ else if(problemNumber == 11){
println!("{}", Problems::Problem11::getDescription()); println!("{}", Problems::Problem11::getDescription());
} }
else if(problemNumber == 12){
println!("{}", Problems::Problem12::getDescription());
}
} }
fn getProblemNumber() -> u32{ fn getProblemNumber() -> u32{
println!("Enter a problem number: "); println!("Enter a problem number: ");