mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Updated for performance
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//ProjectEuler/Java/Problem16.java
|
||||
//Matthew Ellison
|
||||
// Created: 03-04-19
|
||||
//Modified: 03-28-19
|
||||
//Modified: 06-16-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/JavaClasses
|
||||
/*
|
||||
@@ -28,9 +28,9 @@ import java.math.BigInteger;
|
||||
|
||||
public class Problem16 extends Problem{
|
||||
//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
|
||||
private static final Integer POWER = 1000;
|
||||
private static final int POWER = 1000;
|
||||
|
||||
public Problem16(){
|
||||
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(){
|
||||
//Setup the other variables
|
||||
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
|
||||
timer.start();
|
||||
|
||||
//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
|
||||
String numString = num.toString();
|
||||
@@ -65,5 +65,5 @@ public class Problem16 extends Problem{
|
||||
/* Results:
|
||||
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
|
||||
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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user