mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Added solution to problem 20
This commit is contained in:
@@ -42,3 +42,4 @@ pub mod Problem16;
|
||||
pub mod Problem17;
|
||||
pub mod Problem18;
|
||||
pub mod Problem19;
|
||||
pub mod Problem20;
|
||||
|
||||
65
src/Problems/Problem20.rs
Normal file
65
src/Problems/Problem20.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
//ProjectEulerRust/src/Problems/Problems20.rs
|
||||
//Matthew Ellison
|
||||
// Created: 06-17-20
|
||||
//Modified: 06-17-20
|
||||
//What is the sum of the digits of 100!?
|
||||
//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 sum of the digits of 100!?".to_string()
|
||||
}
|
||||
|
||||
pub fn solve() -> Answer{
|
||||
let TOP_NUM = 100;
|
||||
//The number that is being generated
|
||||
let mut number = num::BigInt::from(1);
|
||||
let mut sum = 0;
|
||||
|
||||
//Start the timer
|
||||
let mut timer = myClasses::Stopwatch::Stopwatch::new();
|
||||
timer.start();
|
||||
|
||||
//Run through every number from 1 to 100 and multiply it by the current num to generate 100!
|
||||
for cnt in (2..=TOP_NUM).rev(){
|
||||
number *= num::BigInt::from(cnt);
|
||||
}
|
||||
|
||||
//Get a string of the number because it is easier to pull apart the individual characters
|
||||
let numString = number.to_string();
|
||||
//Run through every character in the string, convert it back to an integer and add it to the running sum
|
||||
for cnt in numString.chars(){
|
||||
sum += cnt.to_digit(10).unwrap();
|
||||
}
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Return the results
|
||||
return Answer::new(format!("{}! = {}\nThe sum of the digits is: {}", TOP_NUM, number, sum), timer.getString());
|
||||
}
|
||||
|
||||
/* Results:
|
||||
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
||||
The sum of the digits is: 648
|
||||
It took 18.000 microseconds to solve this problem
|
||||
*/
|
||||
12
src/main.rs
12
src/main.rs
@@ -33,8 +33,9 @@ mod Problems;
|
||||
#[derive(PartialEq)]
|
||||
enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE}
|
||||
|
||||
static problemNumbers: [u32; 20] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
|
||||
static problemNumbers: [u32; 21] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20];
|
||||
|
||||
fn main(){
|
||||
let mut selection = Selections::EMPTY;
|
||||
@@ -207,6 +208,10 @@ fn solveProblem(problemNumber: u32){
|
||||
println!("{}", Problems::Problem19::getDescription());
|
||||
println!("{}", Problems::Problem19::solve());
|
||||
}
|
||||
else if(problemNumber == 20){
|
||||
println!("{}", Problems::Problem20::getDescription());
|
||||
println!("{}", Problems::Problem20::solve());
|
||||
}
|
||||
}
|
||||
fn descriptionMenu(){
|
||||
//Give some extra space to print the description
|
||||
@@ -289,6 +294,9 @@ fn printDescription(problemNumber: u32){
|
||||
else if(problemNumber == 19){
|
||||
println!("{}", Problems::Problem19::getDescription());
|
||||
}
|
||||
else if(problemNumber == 20){
|
||||
println!("{}", Problems::Problem20::getDescription());
|
||||
}
|
||||
}
|
||||
fn getProblemNumber() -> u32{
|
||||
println!("Enter a problem number: ");
|
||||
|
||||
Reference in New Issue
Block a user