From f600938944f97879d71761de3f85d510c876cc4a Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 16 Jun 2020 19:22:05 -0400 Subject: [PATCH] Added solution to problem 16 --- src/Problems.rs | 1 + src/Problems/Problem16.rs | 66 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 11 +++++-- 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/Problems/Problem16.rs diff --git a/src/Problems.rs b/src/Problems.rs index e408959..e0ae5c6 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -38,3 +38,4 @@ pub mod Problem12; pub mod Problem13; pub mod Problem14; pub mod Problem15; +pub mod Problem16; diff --git a/src/Problems/Problem16.rs b/src/Problems/Problem16.rs new file mode 100644 index 0000000..3129c88 --- /dev/null +++ b/src/Problems/Problem16.rs @@ -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 . +*/ + + +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 +*/ diff --git a/src/main.rs b/src/main.rs index 1886754..adf52b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,8 +33,8 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 16] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15]; +static problemNumbers: [u32; 17] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16]; fn main(){ let mut selection = Selections::EMPTY; @@ -191,6 +191,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem15::getDescription()); println!("{}", Problems::Problem15::solve()); } + else if(problemNumber == 16){ + println!("{}", Problems::Problem16::getDescription()); + println!("{}", Problems::Problem16::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -261,6 +265,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 15){ println!("{}", Problems::Problem15::getDescription()); } + else if(problemNumber == 16){ + println!("{}", Problems::Problem16::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");