From 2fa7cb7a795be34962197791e70cde1d281967fc Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 1 Jun 2021 18:44:27 -0400 Subject: [PATCH] Added solution to problem 34 --- ProjectEulerCS/ProblemSelection.cs | 3 +- ProjectEulerCS/Problems/Problem33.cs | 1 - ProjectEulerCS/Problems/Problem34.cs | 126 +++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 ProjectEulerCS/Problems/Problem34.cs diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index 4f2ac7d..16f5273 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -33,7 +33,7 @@ namespace ProjectEulerCS{ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 67}; + 30, 31, 32, 33, 34, 67}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -75,6 +75,7 @@ namespace ProjectEulerCS{ case 31: problem = new Problem31(); break; case 32: problem = new Problem32(); break; case 33: problem = new Problem33(); break; + case 34: problem = new Problem34(); break; case 67: problem = new Problem67(); break; } return problem; diff --git a/ProjectEulerCS/Problems/Problem33.cs b/ProjectEulerCS/Problems/Problem33.cs index 6621c58..61d545e 100644 --- a/ProjectEulerCS/Problems/Problem33.cs +++ b/ProjectEulerCS/Problems/Problem33.cs @@ -27,7 +27,6 @@ If the product of these four fractions is given in its lowest common terms, find */ -using System; using System.Collections.Generic; diff --git a/ProjectEulerCS/Problems/Problem34.cs b/ProjectEulerCS/Problems/Problem34.cs new file mode 100644 index 0000000..3e2a396 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem34.cs @@ -0,0 +1,126 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem34.cs +//Matthew Ellison +// Created: 06-01-21 +//Modified: 06-01-21 +//Find the sum of all numbers which are equal to the sum of the factorial of their digits +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses +/* + Copyright (C) 2021 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 . +*/ + + +using System; +using System.Collections.Generic; + + +namespace ProjectEulerCS.Problems{ + public class Problem34 : Problem{ + //Variables + //Static variables + private static readonly int MAX_NUM = 1499999; //The largest num that can be the sum of its own digits + //Instance variables + private readonly List factorials; //Holds the pre-computed factorials of the numbers 0-9 + private int sum; //Holds the sum of all numbers equal to the sum of their digit's factorials + //Gets + //The results of the problem + public override string Result{ + get{ + if(!solved){ + throw new Unsolved(); + } + return $"The sum of all numbers that are the sum of their digit's factorials is {sum}"; + } + } + //Returns the list of factorials from 0-9 + public List Factorials{ + get{ + if(!solved){ + throw new Unsolved(); + } + return factorials; + } + } + //Returns the sum of all numbers equal to the sum of their digit's factorials + public int Sum{ + get{ + if(!solved){ + throw new Unsolved(); + } + return sum; + } + } + + //Functions + //Constructor + public Problem34() : base("Find the sum of all numbers which are equal to the sum of the factorial of their digits"){ + sum = 0; + factorials = new List(10); + for(int cnt = 0;cnt <= 9; ++cnt){ + factorials.Add(0); + } + } + //Operational functions + //Solve the problem + public override void Solve(){ + //If the porblem has already been solved do nothing and end the function + if(solved){ + return; + } + + //Start the timer + timer.Start(); + + //Pre-compute the possible factorials from 0! to 9! + for(int cnt = 0;cnt <= 9;++cnt){ + factorials[cnt] = mee.Algorithms.Factorial(cnt); + } + //Run through all possible numbers from 3-MAX_NUM and see if they equal the sum of their digit's factorials + for(int cnt = 3;cnt < MAX_NUM;++cnt){ + //Split the number into its digits and add each one to the sum + string numString = cnt.ToString(); + int currentSum = 0; + foreach(char number in numString){ + int tempNum = (int)char.GetNumericValue(number); + currentSum += factorials[tempNum]; + } + //If the number is equal to the sum add the sum to the running sum + if(currentSum == cnt){ + sum += currentSum; + } + } + + //Stop the timer + timer.Stop(); + + //Throw a flag to show the problem is solved + solved = true; + } + //Reset the problem so it can be run again + public override void Reset(){ + base.Reset(); + sum = 0; + factorials.Clear(); + for(int cnt = 0;cnt <= 9;++cnt){ + factorials.Add(0); + } + } + } +} + +/* Results: +The sum of all numbers that are the sum of their digit's factorials is 40730 +It took an average of 73.852 milliseconds to run this problem through 100 iterations +*/