mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2025-12-06 17:23:57 -05:00
Added solution to problem 16
This commit is contained in:
@@ -31,7 +31,7 @@ namespace ProjectEulerCS{
|
||||
//Holds the valid problem numbers
|
||||
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
|
||||
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15};
|
||||
10, 11, 12, 13, 14, 15, 16};
|
||||
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
||||
get { return _PROBLEM_NUMBERS; }
|
||||
}
|
||||
@@ -55,6 +55,7 @@ namespace ProjectEulerCS{
|
||||
case 13: problem = new Problem13(); break;
|
||||
case 14: problem = new Problem14(); break;
|
||||
case 15: problem = new Problem15(); break;
|
||||
case 16: problem = new Problem16(); break;
|
||||
}
|
||||
return problem;
|
||||
}
|
||||
|
||||
105
ProjectEulerCS/Problems/Problem16.cs
Normal file
105
ProjectEulerCS/Problems/Problem16.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
//ProjectEuler/ProjectEulerCS/src/Problems/Problem16.cs
|
||||
//Matthew Ellison
|
||||
// Created: 08-25-20
|
||||
//Modified: 08-25-20
|
||||
//What is the sum of the digits of the number 2^1000?
|
||||
//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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
using System.Numerics;
|
||||
|
||||
|
||||
namespace ProjectEulerCS.Problems{
|
||||
public class Problem16 : Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private const int NUM_TO_POWER = 2; //The number that is going to be raised to a power
|
||||
private const int POWER = 1000; //The power that the number is going to be raised to
|
||||
//Instance variables
|
||||
private BigInteger num; //The number to be calculated
|
||||
public BigInteger Number{
|
||||
get{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
private int sumOfElements; //The sum of all digits in the number
|
||||
public int Sum{
|
||||
get{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return sumOfElements;
|
||||
}
|
||||
}
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem16() : base("What is the sum of the digits of the number 2^1000?"){
|
||||
num = 0;
|
||||
sumOfElements = 0;
|
||||
}
|
||||
//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();
|
||||
|
||||
//Get the number
|
||||
num = BigInteger.Pow(NUM_TO_POWER, POWER);
|
||||
|
||||
//Get a string of the number
|
||||
string numString = num.ToString();
|
||||
|
||||
//Add up the individual characters of the string
|
||||
for(int cnt = 0;cnt < numString.Length;++cnt){
|
||||
sumOfElements += System.Convert.ToInt32(numString.Substring(cnt, 1));
|
||||
}
|
||||
|
||||
//Stop the timer
|
||||
_timer.Stop();
|
||||
|
||||
//Throw a flag to show the problem is solved
|
||||
solved = true;
|
||||
|
||||
//Save the results
|
||||
_result = NUM_TO_POWER + "^" + POWER + " = " + num + "\n" +
|
||||
"The sum of the elements is " + sumOfElements;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
public override void Reset(){
|
||||
base.Reset();
|
||||
num = 0;
|
||||
sumOfElements = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
|
||||
The sum of the elements is 1366
|
||||
It took an average of 25.002 microseconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user