mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2025-12-06 17:23:57 -05:00
Added solution to problem 9
This commit is contained in:
@@ -30,7 +30,7 @@ namespace ProjectEulerCS{
|
||||
public class ProblemSelection{
|
||||
//Holds the valid problem numbers
|
||||
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
|
||||
{0, 1, 2, 3, 4, 5, 6, 7, 8};
|
||||
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
||||
get { return _PROBLEM_NUMBERS; }
|
||||
}
|
||||
@@ -47,6 +47,7 @@ namespace ProjectEulerCS{
|
||||
case 6: problem = new Problem6(); break;
|
||||
case 7: problem = new Problem7(); break;
|
||||
case 8: problem = new Problem8(); break;
|
||||
case 9: problem = new Problem9(); break;
|
||||
}
|
||||
return problem;
|
||||
}
|
||||
|
||||
134
ProjectEulerCS/Problems/Problem9.cs
Normal file
134
ProjectEulerCS/Problems/Problem9.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
//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
|
||||
*/
|
||||
Reference in New Issue
Block a user