Updated for performance

This commit is contained in:
2020-06-16 19:21:49 -04:00
parent 187dcaedd7
commit c043df9b44

View File

@@ -1,7 +1,7 @@
//ProjectEuler/Java/Problem16.java //ProjectEuler/Java/Problem16.java
//Matthew Ellison //Matthew Ellison
// Created: 03-04-19 // Created: 03-04-19
//Modified: 03-28-19 //Modified: 06-16-20
//What is the sum of the digits of the number 2^1000? //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/JavaClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/* /*
@@ -28,9 +28,9 @@ import java.math.BigInteger;
public class Problem16 extends Problem{ public class Problem16 extends Problem{
//The number that is going to be raised to a power //The number that is going to be raised to a power
private static final Integer NUM_TO_POWER = 2; private static final int NUM_TO_POWER = 2;
//The power that the number is going to be raised to //The power that the number is going to be raised to
private static final Integer POWER = 1000; private static final int POWER = 1000;
public Problem16(){ public Problem16(){
super("What is the sum of the digits of the number 2^1000?"); super("What is the sum of the digits of the number 2^1000?");
@@ -38,13 +38,13 @@ public class Problem16 extends Problem{
public void solve(){ public void solve(){
//Setup the other variables //Setup the other variables
BigInteger num = new BigInteger("0"); //The number to be calculated BigInteger num = new BigInteger("0"); //The number to be calculated
Integer sumOfElements = 0; //The sum of all digits in the number int sumOfElements = 0; //The sum of all digits in the number
//Start the timer //Start the timer
timer.start(); timer.start();
//Get the number //Get the number
num = BigInteger.valueOf(NUM_TO_POWER.longValue()).pow(POWER); num = BigInteger.valueOf(NUM_TO_POWER).pow(POWER);
//Get a string of the number //Get a string of the number
String numString = num.toString(); String numString = num.toString();
@@ -65,5 +65,5 @@ public class Problem16 extends Problem{
/* Results: /* Results:
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
The sum of the elements is 1366 The sum of the elements is 1366
It took 411.699 microseconds to solve this problem. It took 133.800 microseconds to solve this problem.
*/ */