Updated for performance

This commit is contained in:
2020-06-17 11:43:03 -04:00
parent 45ee548615
commit 46b658407f

View File

@@ -1,11 +1,11 @@
//ProjectEuler/Java/Problem19.java
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem20.java
//Matthew Ellison
// Created: 03-14-19
//Modified: 03-28-19
//Modified: 06-17-20
//What is the sum of the digits of 100!?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2019 Matthew Ellison
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
@@ -28,7 +28,7 @@ import java.math.BigInteger;
public class Problem20 extends Problem{
//The largest number that will be multiplied
private static final Integer TOP_NUM = 100;
private static final int TOP_NUM = 100;
public Problem20(){
super("What is the sum of the digits of 100!?");
@@ -36,20 +36,20 @@ public class Problem20 extends Problem{
public void solve(){
//The number that is being generated
BigInteger num = new BigInteger("1");
Long sum = 0L; //The sum of the digits of num
long sum = 0L; //The sum of the digits of num
//Start the timer
timer.start();
//Run through every number from 1 to 100 and multiply it by the current num to generate 100!
for(Integer cnt = TOP_NUM;cnt > 1;--cnt){
for(int cnt = TOP_NUM;cnt > 1;--cnt){
num = num.multiply(BigInteger.valueOf(cnt));
}
//Get a string of the number because it is easier to pull appart the individucal characters
//Get a string of the number because it is easier to pull apart the individual characters
String numString = num.toString();
//Run through every character in the string, convert it back to an integer and add it to the running sum
for(Integer cnt = 0;cnt < numString.length();++cnt){
for(int cnt = 0;cnt < numString.length();++cnt){
Character temp = numString.charAt(cnt);
sum += Integer.valueOf(temp.toString());
}
@@ -65,5 +65,5 @@ public class Problem20 extends Problem{
/* Restuls:
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
The sum of the digits is: 648
It took 375.798 microseconds to solve this problem.
It took 137.199 microseconds to solve this problem.
*/