Added solution to problem 6

This commit is contained in:
2020-06-15 12:32:27 -04:00
parent 38f1de423e
commit f46932594b
3 changed files with 69 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
//ProjectEulerRust/src/Problems.rs //ProjectEulerRust/src/Problems.rs
//Matthew Ellison //Matthew Ellison
// Created: 06-11-20 // Created: 06-11-20
//Modified: 06-13-20 //Modified: 06-15-20
//This file just forwards all the problem modules //This file just forwards all the problem modules
@@ -11,3 +11,4 @@ pub mod Problem2;
pub mod Problem3; pub mod Problem3;
pub mod Problem4; pub mod Problem4;
pub mod Problem5; pub mod Problem5;
pub mod Problem6;

59
src/Problems/Problem6.rs Normal file
View File

@@ -0,0 +1,59 @@
//ProjectEulerRust/src/Problems/Problems6.rs
//Matthew Ellison
// Created: 06-15-20
//Modified: 06-15-20
//Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
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{
"Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.".to_string()
}
pub fn solve() -> Answer{
let START_NUM = 1;
let END_NUM = 100;
//Start the timer
let mut timer = myClasses::Stopwatch::Stopwatch::new();
timer.start();
//Setup the variables
let mut sumOfSquares = 0i64; //Holds the sum of the squares of all the numbers
let mut squareOfSum = 0i64; //Holds the square of the sum of all the numbers
//Run through all number and add them to the appropriate sums
for currentNum in START_NUM..END_NUM{
//Add the square to the correct variable
sumOfSquares += (currentNum * currentNum);
//Add the number to the correct variable to squaring later
squareOfSum += currentNum;
}
//Squaring the sum that needs it
squareOfSum *= squareOfSum;
//Stop the timer
timer.stop();
//Save the results
return Answer::new(format!("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is {}\n", (sumOfSquares - squareOfSum).abs()), timer.getString());
}

View File

@@ -16,7 +16,7 @@ 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; 6] = [0, 1, 2, 3, 4, 5]; static problemNumbers: [u32; 7] = [0, 1, 2, 3, 4, 5, 6];
fn main(){ fn main(){
let mut selection = Selections::EMPTY; let mut selection = Selections::EMPTY;
@@ -133,6 +133,10 @@ fn solveProblem(problemNumber: u32){
println!("{}", Problems::Problem5::getDescription()); println!("{}", Problems::Problem5::getDescription());
println!("{}", Problems::Problem5::solve()); println!("{}", Problems::Problem5::solve());
} }
else if(problemNumber == 6){
println!("{}", Problems::Problem6::getDescription());
println!("{}", Problems::Problem6::solve());
}
} }
fn descriptionMenu(){ fn descriptionMenu(){
//Give some extra space to print the description //Give some extra space to print the description
@@ -173,6 +177,9 @@ fn printDescription(problemNumber: u32){
else if(problemNumber == 5){ else if(problemNumber == 5){
println!("{}", Problems::Problem5::getDescription()); println!("{}", Problems::Problem5::getDescription());
} }
else if(problemNumber == 6){
println!("{}", Problems::Problem6::getDescription());
}
} }
fn getProblemNumber() -> u32{ fn getProblemNumber() -> u32{
println!("Enter a problem number: "); println!("Enter a problem number: ");