diff --git a/ProjectEulerCS/ProblemSelection.cs b/ProjectEulerCS/ProblemSelection.cs index 573ff23..b593b3a 100644 --- a/ProjectEulerCS/ProblemSelection.cs +++ b/ProjectEulerCS/ProblemSelection.cs @@ -32,7 +32,8 @@ namespace ProjectEulerCS{ private static readonly List _PROBLEM_NUMBERS = new List() { 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, 67}; + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 67}; public static System.Collections.Generic.List PROBLEM_NUMBERS{ get { return _PROBLEM_NUMBERS; } } @@ -69,6 +70,7 @@ namespace ProjectEulerCS{ case 26: problem = new Problem26(); break; case 27: problem = new Problem27(); break; case 28: problem = new Problem28(); break; + case 29: problem = new Problem29(); break; case 67: problem = new Problem67(); break; } return problem; diff --git a/ProjectEulerCS/Problems/Problem29.cs b/ProjectEulerCS/Problems/Problem29.cs new file mode 100644 index 0000000..488b8d8 --- /dev/null +++ b/ProjectEulerCS/Problems/Problem29.cs @@ -0,0 +1,123 @@ +//ProjectEuler/ProjectEulerCS/src/Problems/Problem29.cs +//Matthew Ellison +// Created: 10-03-20 +//Modified: 10-03-20 +//How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100? +//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.Collections.Generic; +using System.Numerics; + + +namespace ProjectEulerCS.Problems{ + public class Problem29 : Problem{ + //Variables + //Static variables + private const int BOTTOM_A = 2; //The lowest possible value for a + private const int TOP_A = 100; //The highest possible value for a + private const int BOTTOM_B = 2; //The lowest possible value for b + private const int TOP_B = 100; //The highest possible value for b + //Instance variables + private List unique; //Holds all unique values generated + //Gets + public static int BottomA{ + get{ + return BOTTOM_A; + } + } + public static int BottomB{ + get{ + return BottomB; + } + } + public static int TopA{ + get{ + return TOP_A; + } + } + public static int TopB{ + get{ + return TOP_B; + } + } + public List Unique{ + get{ + //If the problem hasn't been solved throw an exception + if(!solved){ + throw new Unsolved(); + } + return unique; + } + } + public override string Result{ + get{ + if(!solved){ + throw new Unsolved(); + } + return $"The number of unique values generated by a^b for {BOTTOM_A} <= a <= {TOP_A} and {BOTTOM_B} <= b <= {TOP_B} is {unique.Count}"; + } + } + + //Functions + //Constructor + public Problem29() : base($"How many distinct terms are in the sequence generated by a^b for {BOTTOM_A} <= a <= {TOP_A} and {BOTTOM_B} <= b <= {TOP_B}?"){ + unique = new List(); + } + //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; + } + + //Start the timer + timer.Start(); + + //Start with the first A and move towards the top + for(int currentA = BOTTOM_A;currentA <= TOP_A;++currentA){ + //Start with the first B and move towards the top + for(int currentB = BOTTOM_B;currentB <= TOP_B;++currentB){ + //Get the new number + BigInteger currentNum = BigInteger.Pow(currentA, currentB); + //If the current number is not in the array add it + if(!unique.Contains(currentNum)){ + unique.Add(currentNum); + } + } + } + + //Stop the timer + timer.Stop(); + + //Throw a flag to show the problem is solved + solved = true; + } + public override void Reset(){ + base.Reset(); + unique.Clear(); + } + } +} + +/* Results: +The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183 +It took an average of 127.770 milliseconds to run this problem through 100 iterations +*/