mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2025-12-06 17:23:57 -05:00
Added solution to problem 34
This commit is contained in:
@@ -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<int> 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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
|
||||
126
ProjectEulerCS/Problems/Problem34.cs
Normal file
126
ProjectEulerCS/Problems/Problem34.cs
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
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<int> 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<int> 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<int>(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
|
||||
*/
|
||||
Reference in New Issue
Block a user