diff --git a/ProjectEulerCS/Problems/Problem1.cs b/ProjectEulerCS/Problems/Problem1.cs index 2d0c8ab..d3eaf84 100644 --- a/ProjectEulerCS/Problems/Problem1.cs +++ b/ProjectEulerCS/Problems/Problem1.cs @@ -1,7 +1,7 @@ //ProjectEuler/ProjectEulerCS/src/Problems/Problem1.cs //Matthew Ellison // Created: 08-14-20 -//Modified: 08-27-20 +//Modified: 10-26-20 //What is the sum of all the multiples of 3 or 5 that are less than 1000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses /* @@ -22,6 +22,9 @@ */ +using System; + + namespace ProjectEulerCS.Problems{ public class Problem1 : Problem{ //Variables @@ -64,15 +67,8 @@ namespace ProjectEulerCS.Problems{ //Start the timer timer.Start(); - //Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum - for (int cnt = 1; cnt <= TOP_NUM; ++cnt){ - if ((cnt % 3) == 0){ - fullSum += cnt; - } - else if ((cnt % 5) == 0){ - fullSum += cnt; - } - } + //Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap + fullSum = SumOfProgression(3) + SumOfProgression(5) - SumOfProgression(3 * 5); //Stop the timer timer.Stop(); @@ -85,10 +81,15 @@ namespace ProjectEulerCS.Problems{ base.Reset(); fullSum = 0; } + private int SumOfProgression(double multiple){ + double numTerms = Math.Floor(TOP_NUM / multiple); //This gets the number of multiples of a particular number that is < MAX_NUMBER + //The sum of progression formula is (n / 2)(a + l). n = number of terms, a = multiple, l = last term + return (int)(numTerms / 2.0 * (multiple + (numTerms * multiple))); + } } } /* Results: The sum of all numbers < 1000 is 233168 -It took an average of 1.430 microseconds to run this problem through 100 iterations +It took an average of 1.351 microseconds to run this problem through 100 iterations */ \ No newline at end of file