mirror of
https://bitbucket.org/Mattrixwv/projecteulercpp.git
synced 2025-12-06 17:13:59 -05:00
Inital commit with existing files
This commit is contained in:
119
Source/Problem9.cpp
Normal file
119
Source/Problem9.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
//ProjectEuler/C++/Source/Problem9.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 09-28-18
|
||||
//Modified: 07-14-19
|
||||
//There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product of abc.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
|
||||
/*
|
||||
Copyright (C) 2019 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include "Stopwatch.hpp"
|
||||
#include "../Headers/Problem.hpp"
|
||||
#include "../Headers/Problem9.hpp"
|
||||
|
||||
|
||||
Problem9::Problem9() : Problem("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product of abc."), a(1), b(0), c(0), found(false){
|
||||
|
||||
}
|
||||
|
||||
void Problem9::solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
}
|
||||
//Start the timer
|
||||
timer.start();
|
||||
|
||||
//Loop through all possible a's
|
||||
while((a < 1000) && !found){
|
||||
b = a + 1; //b bust be greater than a
|
||||
c = sqrt(pow(a, 2) + pow(b, 2));
|
||||
|
||||
//Loop through all possible b's
|
||||
while((a + b + c) < 1000){
|
||||
++b;
|
||||
c = sqrt(pow(a, 2) + pow(b, 2));
|
||||
}
|
||||
|
||||
//If the sum == 1000 you found the number, otherwise go to the next possible a
|
||||
if((a + b + c) == 1000){
|
||||
found = true;
|
||||
}
|
||||
else{
|
||||
++a;
|
||||
}
|
||||
}
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Throw a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
|
||||
std::string Problem9::getString() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
std::stringstream results;
|
||||
if(found){
|
||||
results << "The Pythagorean triplet is " << a << ' ' << b << ' ' << (int)c
|
||||
<< "\nThe numbers' product is " << a * b * (int)c;
|
||||
}
|
||||
else{
|
||||
results << "The number was not found!";
|
||||
}
|
||||
|
||||
return results.str();
|
||||
}
|
||||
|
||||
int Problem9::getSideA() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
int Problem9::getSideB() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
int Problem9::getSideC() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return (int)c;
|
||||
}
|
||||
|
||||
int Problem9::getProduct() const{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw unsolved();
|
||||
}
|
||||
return a * b * (int)c;
|
||||
}
|
||||
Reference in New Issue
Block a user