From 9326c8355a0e38deb59cc8bd7c9bf9998e006010 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 1 Jun 2021 18:44:19 -0400 Subject: [PATCH] Added solution to problem 34 --- src/Problems.rs | 13 ++++++-- src/Problems/Problem34.rs | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/Problems/Problem34.rs diff --git a/src/Problems.rs b/src/Problems.rs index 3d1ab60..77ccdca 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -56,13 +56,14 @@ pub mod Problem30; pub mod Problem31; pub mod Problem32; pub mod Problem33; +pub mod Problem34; pub mod Problem67; -pub static problemNumbers: [u32; 35] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +pub static problemNumbers: [u32; 36] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 67]; + 30, 31, 32, 33, 34, 67]; pub static tooLong: [u32; 7] = [3, 5, 15, 23, 24, 25, 27]; pub fn solveProblem(problemNumber: u32, description: bool, solve: bool) -> Answer::Answer{ @@ -335,6 +336,14 @@ pub fn solveProblem(problemNumber: u32, description: bool, solve: bool) -> Answe answer = Problem33::solve(); } } + else if(problemNumber == 34){ + if(description){ + println!("{}", Problem34::getDescription()); + } + if(solve){ + answer = Problem34::solve(); + } + } else if(problemNumber == 67){ if(description){ println!("{}", Problem67::getDescription()); diff --git a/src/Problems/Problem34.rs b/src/Problems/Problem34.rs new file mode 100644 index 0000000..89c864d --- /dev/null +++ b/src/Problems/Problem34.rs @@ -0,0 +1,68 @@ +//ProjectEulerRust/src/Problems/Problems34.rs +//Matthew Ellison +// Created: 06-01-21 +//Modified: 06-01-21 +//Find the sum of all numbers which are equal to the sum of the factorial of their digits +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses +/* + Copyright (C) 2021 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{ + "Find the sum of all numbers which are equal to the sum of the factorial of their digits".to_string() +} + +pub fn solve() -> Answer{ + let maxNum = 1499999; + let mut factorials = Vec::::new(); + let mut sum = 0; + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Pre-compute the possible factorials from 0! to 9! + for cnt in 0 ..= 9{ + factorials.push(myClasses::Algorithms::factorial(cnt)); + } + //Run through all possible number form 3-MAX_NUM and see if they equal the sum of their digit's factorials + for cnt in 3 ..= maxNum{ + //Split the number into its digits and add each one to the sum + let numString = cnt.to_string(); + let mut currentSum = 0; + for numChar in numString.chars(){ + currentSum += factorials[numChar.to_digit(10).unwrap() as usize]; + } + //If the number is equal to the sum add the sum to the running sum + if(currentSum == cnt){ + sum += currentSum; + } + } + + //Stop the timer + timer.stop(); + + return Answer::new(format!("The sum of all numbers that are the sum of their digit's factorials is {}", sum), timer.getString(), timer.getNano()); +} + +/* Results +The sum of all numbers that are the sum of their digit's factorials is 40730 +It took an average of 143.433 milliseconds to run this problem through 100 iterations +*/