Updated to better match C# standards

This commit is contained in:
2020-09-04 18:02:09 -04:00
parent cce17c8223
commit 14360b04cf
4 changed files with 15 additions and 15 deletions

View File

@@ -31,8 +31,8 @@ namespace ProjectEulerCS.Problems{
//Static variables //Static variables
private const int LIMIT = 10000; //The number that is > the largest number to be checked private const int LIMIT = 10000; //The number that is > the largest number to be checked
//Instance variables //Instance variables
private List<int> divisorSum; //Holds the sum of the divisors of the subscript number private readonly List<int> divisorSum; //Holds the sum of the divisors of the subscript number
private List<int> amicable; //Holds all amicable numbers private readonly List<int> amicable; //Holds all amicable numbers
public List<int> Amicable{ public List<int> Amicable{
get{ get{
if(!solved){ if(!solved){
@@ -68,11 +68,11 @@ namespace ProjectEulerCS.Problems{
public Problem21() : base($"Evaluate the sum of all the amicable numbers under {LIMIT}"){ public Problem21() : base($"Evaluate the sum of all the amicable numbers under {LIMIT}"){
divisorSum = new List<int>(); divisorSum = new List<int>();
amicable = new List<int>(); amicable = new List<int>();
reserveArray(); ReserveArray();
} }
//Operational functions //Operational functions
//Reserve the size of the array to speed up insertion //Reserve the size of the array to speed up insertion
private void reserveArray(){ private void ReserveArray(){
divisorSum.Capacity = LIMIT; //Reserving it now makes it faster later divisorSum.Capacity = LIMIT; //Reserving it now makes it faster later
//Make sure the list is filled with 0's //Make sure the list is filled with 0's
while(divisorSum.Count < LIMIT){ while(divisorSum.Count < LIMIT){
@@ -129,7 +129,7 @@ namespace ProjectEulerCS.Problems{
base.Reset(); base.Reset();
divisorSum.Clear(); divisorSum.Clear();
amicable.Clear(); amicable.Clear();
reserveArray(); ReserveArray();
} }
} }
} }

View File

@@ -400,8 +400,8 @@ namespace ProjectEulerCS.Problems{
"KENETH","JACINTO","GRAIG","FRANKLYN","EDMUNDO","SID","PORTER","LEIF","JERAMY","BUCK","WILLIAN","VINCENZO","SHON","LYNWOOD","JERE", "KENETH","JACINTO","GRAIG","FRANKLYN","EDMUNDO","SID","PORTER","LEIF","JERAMY","BUCK","WILLIAN","VINCENZO","SHON","LYNWOOD","JERE",
"HAI","ELDEN","DORSEY","DARELL","BRODERICK","ALONSO"}; "HAI","ELDEN","DORSEY","DARELL","BRODERICK","ALONSO"};
//Instance variables //Instance variables
private List<long> sums; //Holds the score based on the sum of the characters in the name private readonly List<long> sums; //Holds the score based on the sum of the characters in the name
private List<long> prod; //Holds the score based on the sum of the characters and the location in alphabetical order private readonly List<long> prod; //Holds the score based on the sum of the characters and the location in alphabetical order
private long sum; //Holds the sum of the scores private long sum; //Holds the sum of the scores
public List<string> Names{ public List<string> Names{
get{ get{

View File

@@ -31,7 +31,7 @@ namespace ProjectEulerCS.Problems{
//Static variables //Static variables
private const int MAX_NUM = 28123; //THe largest number to be checked private const int MAX_NUM = 28123; //THe largest number to be checked
//Instance variables //Instance variables
private List<int> divisorSums; //This gives the sum of the divisors at subscripts private readonly List<int> divisorSums; //This gives the sum of the divisors at subscripts
private long sum; //The sum of all the numbers we are looking for private long sum; //The sum of all the numbers we are looking for
public override string Result{ public override string Result{
get{ get{
@@ -54,12 +54,12 @@ namespace ProjectEulerCS.Problems{
//Constructor //Constructor
public Problem23() : base("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers"){ public Problem23() : base("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers"){
divisorSums = new List<int>(); divisorSums = new List<int>();
reserveArray(); ReserveArray();
sum = 0; sum = 0;
} }
//Operational functions //Operational functions
//Reserve the size of the array to speed up insertion //Reserve the size of the array to speed up insertion
private void reserveArray(){ private void ReserveArray(){
divisorSums.Capacity = MAX_NUM; //It is faster to reserve the appropriate amount of ram now divisorSums.Capacity = MAX_NUM; //It is faster to reserve the appropriate amount of ram now
//Make sure every element has a 0 in it's location //Make sure every element has a 0 in it's location
while(divisorSums.Count <= MAX_NUM){ while(divisorSums.Count <= MAX_NUM){
@@ -96,7 +96,7 @@ namespace ProjectEulerCS.Problems{
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no //Check if each number can be the sum of 2 abundant numbers and add to the sum if no
for(int cnt = 1;cnt < MAX_NUM;++cnt){ for(int cnt = 1;cnt < MAX_NUM;++cnt){
if(!isSum(abund, cnt)){ if(!IsSum(abund, cnt)){
sum += cnt; sum += cnt;
} }
} }
@@ -108,8 +108,8 @@ namespace ProjectEulerCS.Problems{
solved = true; solved = true;
} }
//A function that returns true if num can be created by adding two elements from abund and false if it cannot //A function that returns true if num can be created by adding two elements from abund and false if it cannot
private bool isSum(List<int> abund, int num){ private bool IsSum(List<int> abund, int num){
int sum = 0; int sum;
//Pick a number for the first part of the sum //Pick a number for the first part of the sum
for(int firstNum = 0;firstNum < abund.Count;++firstNum){ for(int firstNum = 0;firstNum < abund.Count;++firstNum){
//Pick a number for the second part of the sum //Pick a number for the second part of the sum
@@ -130,7 +130,7 @@ namespace ProjectEulerCS.Problems{
public override void Reset(){ public override void Reset(){
base.Reset(); base.Reset();
divisorSums.Clear(); divisorSums.Clear();
reserveArray(); ReserveArray();
sum = 0; sum = 0;
} }
} }

View File

@@ -30,7 +30,7 @@ namespace ProjectEulerCS.Problems{
//Variables //Variables
//Static variables //Static variables
private const int NEEDED_PERM = 1000000; //The number of the permutation that you need private const int NEEDED_PERM = 1000000; //The number of the permutation that you need
private static string nums = "0123456789"; //All of the characters that we need to get the permutations of private const string nums = "0123456789"; //All of the characters that we need to get the permutations of
//Instance variables //Instance variables
private List<string> permutations; //Holds all of the permutations of the string nums private List<string> permutations; //Holds all of the permutations of the string nums
public List<string> PermutationsList{ public List<string> PermutationsList{