//ProjectEuler/Java/Problem19.java //Matthew Ellison // Created: 03-14-19 //Modified: 03-28-19 //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 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 . */ import mattrixwv.Stopwatch; import java.math.BigInteger; public class Problem20{ private static Integer TOP_NUM = 100; public static void main(String[] argv){ Stopwatch timer = new Stopwatch(); BigInteger num = new BigInteger("1"); //The number that is being generated 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){ num = num.multiply(BigInteger.valueOf(cnt)); } //Get a string of the number because it is easier to pull appart the individucal 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){ Character temp = numString.charAt(cnt); sum += Integer.valueOf(temp.toString()); } //Stop the timer timer.stop(); //Print the results System.out.printf("%d! = %s\n", TOP_NUM, num.toString()); System.out.printf("The sum of the digits is: %d\n", sum); System.out.println("It took " + timer.getStr() + " to run this algorithm"); } } /* Restuls: 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 The sum of the digits is: 648 It took 2.667 milliseconds to run this algorithm */