From fd9bed661f90567e96693129c24a936b326e872b Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 1 Jun 2021 18:44:26 -0400 Subject: [PATCH] Added solution to problem 34 --- .../ProjectEuler/ProblemSelection.java | 3 +- .../ProjectEuler/Problems/Problem34.java | 125 ++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java diff --git a/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java index a1fa721..96f8fe7 100644 --- a/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java +++ b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java @@ -36,7 +36,7 @@ public class ProblemSelection{ public static final ArrayList PROBLEM_NUMBERS = new ArrayList(Arrays.asList( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, - 31, 32, 33, 67)); + 31, 32, 33, 34, 67)); //Returns the problem corresponding to the given problem number public static Problem getProblem(Integer problemNumber){ @@ -75,6 +75,7 @@ public class ProblemSelection{ case 31 : problem = new Problem31(); break; case 32 : problem = new Problem32(); break; case 33 : problem = new Problem33(); break; + case 34 : problem = new Problem34(); break; case 67 : problem = new Problem67(); break; } return problem; diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java new file mode 100644 index 0000000..36ea37f --- /dev/null +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java @@ -0,0 +1,125 @@ +//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java +//Matthew Ellison +// Created: 02-05-21 +//Modified: 02-07-21 +//Find the sum of all numbers which are equal to the sum of the factorial of their digits +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses +/* + Copyright (C) 2021 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 . +*/ +package mattrixwv.ProjectEuler.Problems; + +import java.util.ArrayList; + +import mattrixwv.Algorithms; +import mattrixwv.ProjectEuler.Unsolved; + +public class Problem34 extends Problem{ + //Variables + //Static variables + private static final int MAX_NUM = 1499999; //The largest num that can be the sum of its own digits + //Instance variables + private ArrayList factorials; //Holds the pre-computed factorials of the numbers 0-9 + private int sum; //Holds the sum of all numbers equal to the sum of their digit's factorials + + //Functions + //Constructor + public Problem34(){ + super("Find the sum of all numbers which are equal to the sum of the factorial of their digits"); + sum = 0; + factorials = new ArrayList(); + for(int cnt = 0;cnt <= 9;++cnt){ + factorials.add(0); + } + } + //Operational functions + //Solve the problem + public void solve(){ + //If the problem has already been solved do nothing and end the function + if(solved){ + return; + } + + //Start the timer + timer.start(); + + //Pre-compute the possible factorials from 0! to 9! + for(int cnt = 0;cnt <= 9;++cnt){ + factorials.set(cnt, Algorithms.factorial(cnt)); + } + //Run through all possible numbers from 3-MAX_NUM and see if they equal the sum of their digit's factorials + for(int cnt = 3;cnt < MAX_NUM;++cnt){ + //Split the number into its digits and add each one to the sum + String numString = "" + cnt; + int currentSum = 0; + for(int charCnt = 0;charCnt < numString.length();++charCnt){ + Character digit = numString.charAt(charCnt); + int tempNum = Integer.valueOf(digit.toString()); + currentSum += factorials.get(tempNum); + } + //If the number is equal to the sum add the sum to the running sum + if(currentSum == cnt){ + sum += currentSum; + } + } + + //Stop the timer + timer.stop(); + + //Throw a flag to show the problem is solved + solved = true; + } + //Reset the proble so it can be run again + public void reset(){ + super.reset(); + sum = 0; + factorials = new ArrayList(); + for(int cnt = 0;cnt <= 9;++cnt){ + factorials.add(0); + } + } + + //Gets + //Returns a string witht he solution to the problem + public String getResult(){ + //If the problem hasn't been solved throw an exception + if(!solved){ + throw new Unsolved(); + } + return String.format("The sum of all numbers that are the sum of their digit's factorials is %d", sum); + } + //Returns the list of factorials from 0-9 + public ArrayList getFactorials(){ + //If the problem hasn't been solved throw an exception + if(!solved){ + throw new Unsolved(); + } + return factorials; + } + //Returns the sum of all numbers equal to the sum of their digit's factorials + public int getSum(){ + //If the problem hasn't been solved throw an exception + if(!solved){ + throw new Unsolved(); + } + return sum; + } +} + +/* Results: +The sum of all numbers that are the sum of their digit's factorials is 40730 +It took an average of 78.889 milliseconds to run this problem through 100 iterations +*/