mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2025-12-06 17:23:57 -05:00
129 lines
3.4 KiB
C#
129 lines
3.4 KiB
C#
//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
|
|
*/
|