diff --git a/src/Problems.rs b/src/Problems.rs index c136a4e..164152b 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -47,3 +47,4 @@ pub mod Problem21; pub mod Problem22; pub mod Problem23; pub mod Problem24; +pub mod Problem25; diff --git a/src/Problems/Problem25.rs b/src/Problems/Problem25.rs new file mode 100644 index 0000000..542faca --- /dev/null +++ b/src/Problems/Problem25.rs @@ -0,0 +1,61 @@ +//ProjectEulerRust/src/Problems/Problems25.rs +//Matthew Ellison +// Created: 06-17-20 +//Modified: 06-17-20 +//What is the index of the first term in the Fibonacci sequence to contain 1000 digits? +//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{ + "What is the index of the first term in the Fibonacci sequence to contain 1000 digits?".to_string() +} + +pub fn solve() -> Answer{ + let NUM_DIGITS = 1000; + //Setup the variables + let mut number = num::BigInt::from(0); + let mut index = num::BigInt::from(2); + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Move through all Fibonacci numbers unti lyou reach the one with as least NUM_DIGITS digits + while(number.to_str_radix(10).len() < NUM_DIGITS){ + //Increase the index number. Doing this at the beginning keeps the index correct at the end of the loop + index += 1; + //Calculate the number + number = myClasses::Algorithms::getFibBig(num::BigInt::new(index.to_u32_digits().0, index.to_u32_digits().1)); + } + + //Stop the timer + timer.stop(); + + //Return the results + return Answer::new(format!("The first Fibonacci number with {} digits is {}\nIts index is {}", NUM_DIGITS, number, index), timer.getString()); +} + +/* Results: +The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816 +Its index is 4782 +It took 1.681 seconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index 7e50c98..8ce278d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,9 +33,9 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 25] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +static problemNumbers: [u32; 26] = [ 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]; + 20, 21, 22, 23, 24, 25]; fn main(){ let mut selection = Selections::EMPTY; @@ -228,6 +228,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem24::getDescription()); println!("{}", Problems::Problem24::solve()); } + else if(problemNumber == 25){ + println!("{}", Problems::Problem25::getDescription()); + println!("{}", Problems::Problem25::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -325,6 +329,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 24){ println!("{}", Problems::Problem24::getDescription()); } + else if(problemNumber == 25){ + println!("{}", Problems::Problem25::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");