From f46932594b745cf0e5b926625d6dc9c805588a02 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 15 Jun 2020 12:32:27 -0400 Subject: [PATCH] Added solution to problem 6 --- src/Problems.rs | 3 +- src/Problems/Problem6.rs | 59 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 +++++- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/Problems/Problem6.rs diff --git a/src/Problems.rs b/src/Problems.rs index 8fb87a3..9f02bd9 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -1,7 +1,7 @@ //ProjectEulerRust/src/Problems.rs //Matthew Ellison // Created: 06-11-20 -//Modified: 06-13-20 +//Modified: 06-15-20 //This file just forwards all the problem modules @@ -11,3 +11,4 @@ pub mod Problem2; pub mod Problem3; pub mod Problem4; pub mod Problem5; +pub mod Problem6; diff --git a/src/Problems/Problem6.rs b/src/Problems/Problem6.rs new file mode 100644 index 0000000..e4dfaf5 --- /dev/null +++ b/src/Problems/Problem6.rs @@ -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 . +*/ + + +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()); +} diff --git a/src/main.rs b/src/main.rs index f8b9d29..cc9cd82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod Problems; #[derive(PartialEq)] 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(){ let mut selection = Selections::EMPTY; @@ -133,6 +133,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem5::getDescription()); println!("{}", Problems::Problem5::solve()); } + else if(problemNumber == 6){ + println!("{}", Problems::Problem6::getDescription()); + println!("{}", Problems::Problem6::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -173,6 +177,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 5){ println!("{}", Problems::Problem5::getDescription()); } + else if(problemNumber == 6){ + println!("{}", Problems::Problem6::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");