mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2025-12-06 17:23:57 -05:00
Added solution to problem 12
This commit is contained in:
@@ -31,7 +31,7 @@ namespace ProjectEulerCS{
|
|||||||
//Holds the valid problem numbers
|
//Holds the valid problem numbers
|
||||||
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
|
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
|
||||||
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
10, 11};
|
10, 11, 12};
|
||||||
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
||||||
get { return _PROBLEM_NUMBERS; }
|
get { return _PROBLEM_NUMBERS; }
|
||||||
}
|
}
|
||||||
@@ -51,6 +51,7 @@ namespace ProjectEulerCS{
|
|||||||
case 9: problem = new Problem9(); break;
|
case 9: problem = new Problem9(); break;
|
||||||
case 10: problem = new Problem10(); break;
|
case 10: problem = new Problem10(); break;
|
||||||
case 11: problem = new Problem11(); break;
|
case 11: problem = new Problem11(); break;
|
||||||
|
case 12: problem = new Problem12(); break;
|
||||||
}
|
}
|
||||||
return problem;
|
return problem;
|
||||||
}
|
}
|
||||||
|
|||||||
128
ProjectEulerCS/Problems/Problem12.cs
Normal file
128
ProjectEulerCS/Problems/Problem12.cs
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
//ProjectEuler/ProjectEulerCS/src/Problems/Problem12.cs
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 08-24-20
|
||||||
|
//Modified: 08-24-20
|
||||||
|
//What is the value of the first triangle number to have over five hundred divisors?
|
||||||
|
//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.Problems{
|
||||||
|
public class Problem12 : Problem{
|
||||||
|
//Variables
|
||||||
|
//Static variables
|
||||||
|
private const int GOAL_DIVISORS = 500;
|
||||||
|
|
||||||
|
//Instance variables
|
||||||
|
private long sum; //The sum of the numbers up to counter
|
||||||
|
private long counter; //The next number to be added to sum
|
||||||
|
private List<long> divisors; //Holds the divisors of the triangular number sum
|
||||||
|
public long TriangularNumber{
|
||||||
|
get{
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public long LastNumberAdded{
|
||||||
|
get{
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return counter - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<long> DivisorsOfTriangularNumber{
|
||||||
|
get{
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return divisors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int NumberOfDivisors{
|
||||||
|
get{
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return divisors.Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
|
public Problem12() : base("What is the value of the first triangle number to have over five hundred divisors?"){
|
||||||
|
sum = 1;
|
||||||
|
counter = 2;
|
||||||
|
divisors = 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Setup the other variables
|
||||||
|
bool foundNumber = false; //To flag whether the number has been found
|
||||||
|
|
||||||
|
//Start the timer
|
||||||
|
_timer.start();
|
||||||
|
|
||||||
|
//Loop until you fin the appropriate number
|
||||||
|
while((!foundNumber) && (sum > 0)){
|
||||||
|
divisors = mee.Algorithms.getDivisors(sum);
|
||||||
|
//If the number of divisors is correct set the flag
|
||||||
|
if(divisors.Count > GOAL_DIVISORS){
|
||||||
|
foundNumber = true;
|
||||||
|
}
|
||||||
|
//Otherwise add to the sum and increase the next number
|
||||||
|
else{
|
||||||
|
sum += counter;
|
||||||
|
++counter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Stop the timer
|
||||||
|
_timer.stop();
|
||||||
|
|
||||||
|
//Throw a flag to show the problem is sovled
|
||||||
|
solved = true;
|
||||||
|
|
||||||
|
//Save the results
|
||||||
|
_result = "The triangular number " + sum + " is the sum of all number >= " + (counter - 1) + " and has " + divisors.Count + " divisors";
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public override void reset(){
|
||||||
|
base.reset();
|
||||||
|
sum = 1;
|
||||||
|
counter = 2;
|
||||||
|
divisors.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Results:
|
||||||
|
The triangular number 76576500 is the sum of all number >= 12375 and has 576 divisors
|
||||||
|
It took an average of 270.496 milliseconds to run this problem through 100 iterations
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user