Added solution to problem 29

This commit is contained in:
2020-06-17 23:21:49 -04:00
parent 13fda69f20
commit 4b083dd6cc
3 changed files with 77 additions and 2 deletions

View File

@@ -51,3 +51,4 @@ pub mod Problem25;
pub mod Problem26; pub mod Problem26;
pub mod Problem27; pub mod Problem27;
pub mod Problem28; pub mod Problem28;
pub mod Problem29;

67
src/Problems/Problem29.rs Normal file
View File

@@ -0,0 +1,67 @@
//ProjectEulerRust/src/Problems/Problems29.rs
//Matthew Ellison
// Created: 06-17-20
//Modified: 06-17-20
//How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 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{
"How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?".to_string()
}
pub fn solve() -> Answer{
//Setup the variables
let BOTTOM_A = 2; //The lowest possible value for a
let TOP_A = 100; //The highest possible value for a
let BOTTOM_B = 2; //The lowest possible value for b
let TOP_B = 100; //The highest possible value for b
let mut unique = Vec::<num::BigInt>::new(); //Holds all unique values generated
//Start the timer
let mut timer = myClasses::Stopwatch::Stopwatch::new();
timer.start();
//Start with the first A and move towards the top
for currentA in BOTTOM_A..=TOP_A{
//Start with the first B and move towards the top
for currentB in BOTTOM_B..=TOP_B{
//Get the new number
let currentNum = num::pow::pow(num::BigInt::from(currentA), currentB);
//If the current number is not int he array add it
if(!unique.contains(&currentNum)){
unique.push(currentNum);
}
}
}
//Stop the timer
timer.stop();
//Return the restuls
return Answer::new(format!("The number of unique values generated by a^b for {} <= a <= {} and {} <= b <= {} is {}", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.len()), timer.getString());
}
/* Result:
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
It took 107.204 milliseconds to solve this problem
*/

View File

@@ -33,9 +33,9 @@ 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; 29] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, static problemNumbers: [u32; 30] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28]; 20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
fn main(){ fn main(){
let mut selection = Selections::EMPTY; let mut selection = Selections::EMPTY;
@@ -244,6 +244,10 @@ fn solveProblem(problemNumber: u32){
println!("{}", Problems::Problem28::getDescription()); println!("{}", Problems::Problem28::getDescription());
println!("{}", Problems::Problem28::solve()); println!("{}", Problems::Problem28::solve());
} }
else if(problemNumber == 29){
println!("{}", Problems::Problem29::getDescription());
println!("{}", Problems::Problem29::solve());
}
} }
fn descriptionMenu(){ fn descriptionMenu(){
//Give some extra space to print the description //Give some extra space to print the description
@@ -353,6 +357,9 @@ fn printDescription(problemNumber: u32){
else if(problemNumber == 28){ else if(problemNumber == 28){
println!("{}", Problems::Problem28::getDescription()); println!("{}", Problems::Problem28::getDescription());
} }
else if(problemNumber == 29){
println!("{}", Problems::Problem29::getDescription());
}
} }
fn getProblemNumber() -> u32{ fn getProblemNumber() -> u32{
println!("Enter a problem number: "); println!("Enter a problem number: ");