mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2025-12-06 17:23:57 -05:00
Added solution to problem 3
This commit is contained in:
@@ -27,7 +27,7 @@ using System.Collections.Generic;
|
||||
namespace ProjectEulerCS{
|
||||
public class ProblemSelection{
|
||||
//Holds the valid problem numbers
|
||||
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>() { 0, 1, 2 };
|
||||
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>() { 0, 1, 2, 3 };
|
||||
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
||||
get { return _PROBLEM_NUMBERS; }
|
||||
}
|
||||
@@ -38,6 +38,7 @@ namespace ProjectEulerCS{
|
||||
switch(problemNumber){
|
||||
case 1: problem = new Problem1(); break;
|
||||
case 2: problem = new Problem2(); break;
|
||||
case 3: problem = new Problem3(); break;
|
||||
}
|
||||
return problem;
|
||||
}
|
||||
|
||||
95
ProjectEulerCS/Problems/Problem3.cs
Normal file
95
ProjectEulerCS/Problems/Problem3.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
//ProjectEuler/ProjectEulerCS/src/Problems/Problem3.cs
|
||||
//Matthew Ellison
|
||||
// Created: 08-23-20
|
||||
//Modified: 08-23-20
|
||||
//The largest prime factor of 600851475143
|
||||
//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/>.
|
||||
*/
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace ProjectEulerCS{
|
||||
public class Problem3 : Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private const long GOAL_NUMBER = 600851475143L; //The number that needs factored
|
||||
public long goalNumber{
|
||||
get{ return GOAL_NUMBER; }
|
||||
}
|
||||
//Instance variables
|
||||
private List<long> _factors; //Holds the factors of goalNumber
|
||||
public List<long> factors{
|
||||
get{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return _factors;
|
||||
}
|
||||
}
|
||||
public long largestFactor{
|
||||
get{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return _factors[_factors.Count - 1];
|
||||
}
|
||||
}
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem3() : base("What is the largest prime factor of 600851475143?"){
|
||||
_factors = new List<long>();
|
||||
}
|
||||
//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;
|
||||
}
|
||||
|
||||
//Star the timer
|
||||
_timer.start();
|
||||
|
||||
//Get all the factors of the number
|
||||
_factors = mee.Algorithms.getFactors(GOAL_NUMBER);
|
||||
//THe last element should be the largest factor
|
||||
|
||||
//Stop the timer
|
||||
_timer.stop();
|
||||
|
||||
//Throw a flag to show the problem is solved
|
||||
solved = true;
|
||||
|
||||
//Save the results
|
||||
_result = "The largest factor of the number " + GOAL_NUMBER + " is " + factors[factors.Count - 1];
|
||||
}
|
||||
//Reset the porblem so it can be run again
|
||||
public override void reset(){
|
||||
base.reset();
|
||||
_factors.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*Results:
|
||||
The largest factor of the number 600851475143 is 6857
|
||||
It took an average of 47.412 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user