mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2025-12-06 17:23:57 -05:00
135 lines
3.3 KiB
C#
135 lines
3.3 KiB
C#
//ProjectEuler/ProjectEulerCS/src/Problems/Problem9.cs
|
|
//Matthew Ellison
|
|
// Created: 08-23-20
|
|
//Modified: 08-23-20
|
|
//There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
|
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
|
|
/*
|
|
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/>.
|
|
*/
|
|
|
|
|
|
namespace ProjectEulerCS{
|
|
public class Problem9 : Problem{
|
|
//Variables
|
|
//Instance variables
|
|
private int a; //Holds the size of the first side
|
|
public int sideA{
|
|
get{
|
|
if(!solved){
|
|
throw new Unsolved();
|
|
}
|
|
return a;
|
|
}
|
|
}
|
|
private int b; //Holds the size of the second side
|
|
public int sideB{
|
|
get{
|
|
if(!solved){
|
|
throw new Unsolved();
|
|
}
|
|
return b;
|
|
}
|
|
}
|
|
private double c; //Holds the size of the hyp
|
|
public int sideC{
|
|
get{
|
|
if(!solved){
|
|
throw new Unsolved();
|
|
}
|
|
return (int)c;
|
|
}
|
|
}
|
|
private bool found; //A flag to determine if we have found the solution yet
|
|
public int product{
|
|
get{
|
|
if(!solved){
|
|
throw new Unsolved();
|
|
}
|
|
return a * b * (int)c;
|
|
}
|
|
}
|
|
|
|
//Functions
|
|
//Constrcutor
|
|
public Problem9() : base("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc."){
|
|
a = 1;
|
|
b = 0;
|
|
c = 0;
|
|
found = false;
|
|
}
|
|
//Operational functions
|
|
//Solve the problem
|
|
public override void 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 must be larget than a
|
|
c = System.Math.Sqrt((a * a) + (b * b)); //Compute the hyp
|
|
|
|
//Loop through all possible b's for this a
|
|
while((a + b + c) < 1000){
|
|
++b;
|
|
c = System.Math.Sqrt((a * a) + (b * b));
|
|
}
|
|
|
|
//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;
|
|
|
|
if(found){
|
|
_result = "The Pythagorean triplet is " + a + " + " + b + " + " + System.Math.Round(c) +
|
|
"\nThe numbers' product is " + product;
|
|
}
|
|
else{
|
|
_result = "The number was not found!";
|
|
}
|
|
}
|
|
//Reset the problem so it can be run again
|
|
public override void reset(){
|
|
base.reset();
|
|
a = 1;
|
|
b = 0;
|
|
c = 0;
|
|
found = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Results:
|
|
The Pythagorean triplet is 200 + 375 + 425
|
|
The numbers' product is 31875000
|
|
It took an average of 149.040 microseconds to run this problem through 100 iterations
|
|
*/
|