Added solution to problem 16

This commit is contained in:
2020-06-16 19:22:05 -04:00
parent 7faccbcade
commit f600938944
3 changed files with 76 additions and 2 deletions

View File

@@ -38,3 +38,4 @@ pub mod Problem12;
pub mod Problem13; pub mod Problem13;
pub mod Problem14; pub mod Problem14;
pub mod Problem15; pub mod Problem15;
pub mod Problem16;

66
src/Problems/Problem16.rs Normal file
View File

@@ -0,0 +1,66 @@
//ProjectEulerRust/src/Problems/Problems16.rs
//Matthew Ellison
// Created: 06-16-20
//Modified: 06-16-20
//What is the sum of the digits of the number 2^1000?
//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{
"Which starting number, under one million, produces the longest chain using the itterative sequence?".to_string()
}
pub fn solve() -> Answer{
//The number that is going to be raised to a power
let NUM_TO_POWER = 2;
//The pwer that the number is going to be raised to
let POWER = 1000;
//The number to be calculated
let mut sumOfElements = 0u64;
//Start the timer
let mut timer = myClasses::Stopwatch::Stopwatch::new();
timer.start();
//Get the number
let number = num::pow::pow(num::BigInt::from(NUM_TO_POWER), POWER);
//Get a string of the number
let numString = number.to_str_radix(10);
//Add up the individual characters of the string
for n in numString.chars(){
sumOfElements += n.to_digit(10).unwrap() as u64;
}
//Stop the timer
timer.stop();
//Return the results
return Answer::new(format!("{}^{} = {}\nThe sum of the elements is {}", NUM_TO_POWER, POWER, number, sumOfElements), timer.getString());
}
/* Results:
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
The sum of the elements is 1366
It took 8.200 microseconds to solve this problem
*/

View File

@@ -33,8 +33,8 @@ 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; 16] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, static problemNumbers: [u32; 17] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15]; 10, 11, 12, 13, 14, 15, 16];
fn main(){ fn main(){
let mut selection = Selections::EMPTY; let mut selection = Selections::EMPTY;
@@ -191,6 +191,10 @@ fn solveProblem(problemNumber: u32){
println!("{}", Problems::Problem15::getDescription()); println!("{}", Problems::Problem15::getDescription());
println!("{}", Problems::Problem15::solve()); println!("{}", Problems::Problem15::solve());
} }
else if(problemNumber == 16){
println!("{}", Problems::Problem16::getDescription());
println!("{}", Problems::Problem16::solve());
}
} }
fn descriptionMenu(){ fn descriptionMenu(){
//Give some extra space to print the description //Give some extra space to print the description
@@ -261,6 +265,9 @@ fn printDescription(problemNumber: u32){
else if(problemNumber == 15){ else if(problemNumber == 15){
println!("{}", Problems::Problem15::getDescription()); println!("{}", Problems::Problem15::getDescription());
} }
else if(problemNumber == 16){
println!("{}", Problems::Problem16::getDescription());
}
} }
fn getProblemNumber() -> u32{ fn getProblemNumber() -> u32{
println!("Enter a problem number: "); println!("Enter a problem number: ");