From aadba28774ccf41debb32e81543e8ecb4b70d836 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sat, 3 Oct 2020 11:43:33 -0400 Subject: [PATCH] Added solution to problem 30 --- ProjectEulerCS/ProblemSelection.cs | 3 +- ProjectEulerCS/Problems/Problem30.cs | 133 +++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 ProjectEulerCS/Problems/Problem30.cs diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index b593b3a..c458562 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, - 67}; + 30, 67}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -71,6 +71,7 @@ namespace ProjectEulerCS{ case 27: problem = new Problem27(); break; case 28: problem = new Problem28(); break; case 29: problem = new Problem29(); break; + case 30: problem = new Problem30(); break; case 67: problem = new Problem67(); break; } return problem; diff --git a/ProjectEulerCS/Problems/Problem30.cs b/ProjectEulerCS/Problems/Problem30.cs new file mode 100644 index 0000000..27fc2a8 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem30.cs @@ -0,0 +1,133 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem30.cs +//Matthew Ellison +// Created: 10-03-20 +//Modified: 10-03-20 +//Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits. +//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 . +*/ + + +using System; +using System.Collections.Generic; + + +namespace ProjectEulerCS.Problems{ + public class Problem30 : Problem{ + //Variables + //Static variables + private const long TOP_NUM = 1000000; //This is the largest number that will be checked + private const long BOTTOM_NUM = 2; //Starts with 2 because 0 and 1 don't count + private const long POWER_RAISED = 5; //Starts with 2 because 0 and 1 don't count + //Instance variables + private List sumOfFifthNumbers; //This is an ArrayList of the numbers that are the sum of the fifth power of their digits + //Gets + public long TopNum{ + get{ + if(!solved){ + throw new Unsolved(); + } + return TOP_NUM; + } + } + public List ListOfSumOfFifths{ + get{ + if(!solved){ + throw new Unsolved(); + } + return sumOfFifthNumbers; + } + } + public long GetSumOfList{ + get{ + if(!solved){ + throw new Unsolved(); + } + return mee.Algorithms.GetSum(sumOfFifthNumbers); + } + } + public override string Result{ + get{ + if(!solved){ + throw new Unsolved(); + } + return $"The sum of all the numbers that can be written as the sum of the fifth powers of their digits is {GetSumOfList}"; + } + } + + //Functions + //Operational functions + public Problem30() : base("Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits."){ + sumOfFifthNumbers = new List(); + } + //Operational functions + //Returns an ArrayList with the individual digits of the number passed to it + private List getDigits(long num){ + List listOfDigits = new List(); //This ArrayList holds the individual digits of num + //The easiest way to get the individual digits of a number is by converting it to a string + string digits = num.ToString(); + //Start with the first digit, convert it to an integer, store it in the ArrayList, and move to the next digit + for(int cnt = 0;cnt < digits.Length;++cnt){ + listOfDigits.Add((long)char.GetNumericValue(digits[cnt])); + } + //Return the list of digits + return listOfDigits; + } + //Solve the problem + public override void Solve(){ + //If the problem has already been solved do nothing and end the function + if(solved){ + return; + } + + //Start the timer + timer.Start(); + + //Start with the lowest number and increment until you reach the largest number + for(long currentNum = BOTTOM_NUM;currentNum <= TOP_NUM;++currentNum){ + //Get the digits of the number + List digits = getDigits(currentNum); + //Get the sum of the powers + long sumOfPowers = 0; + foreach(long num in digits){ + sumOfPowers += (long)Math.Round(Math.Pow(num, POWER_RAISED)); + } + //Check if the sum of the powers is the same as the number + //If it is, add it tot he list, owtherwise continue to the next number + if(sumOfPowers == currentNum){ + sumOfFifthNumbers.Add(currentNum); + } + } + + //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(); + sumOfFifthNumbers.Clear(); + } + } +} + +/* Results: +The sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839 +It took an average of 217.218 milliseconds to run this problem through 100 iterations +*/