Updated for performance

This commit is contained in:
2020-06-17 23:53:26 -04:00
parent bac055495e
commit da5f9bd75d

View File

@@ -1,11 +1,11 @@
//ProjectEuler/Java/Problem30.java
//Matthew
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem30.java
//Matthew Ellison
// Created: 10-27-19
//Modified: 10-27-19
//Modified: 06-17-20
//Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
//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,11 +28,11 @@ import java.util.ArrayList;
public class Problem30 extends Problem{
//This is the largest number that will be checked
private static final Long TOP_NUM = 1000000L;
private static final long TOP_NUM = 1000000L;
//Starts with 2 because 0 and 1 don't count
private static final Long BOTTOM_NUM = 2L;
private static final long BOTTOM_NUM = 2L;
//This is the power that the digits are raised to
private static final Long POWER_RAISED = 5L;
private static final long POWER_RAISED = 5L;
//This is an ArrayList of the numbers that are the sum of the fifth power of their digits
private static ArrayList<Long> sumOfFifthNumbers;
@@ -40,21 +40,21 @@ public class Problem30 extends Problem{
super("Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.");
}
//Returns an ArrayList with the individual digits of the number passed to it
private ArrayList<Long> getDigits(Long num){
private ArrayList<Long> getDigits(long num){
ArrayList<Long> listOfDigits = new ArrayList<Long>(); //This ArrayList holds the individual digits of num
//The easiest way to get the individual digits of a number is by converting it to a string
String digits = num.toString();
String digits = Long.toString(num);
//Start with the first digit, convert it to an integer, store it in the ArrayList, and move to the next digit
for(Integer cnt = 0;cnt < digits.length();++cnt){
for(int cnt = 0;cnt < digits.length();++cnt){
listOfDigits.add(Long.valueOf(digits.substring(cnt, cnt + 1)));
}
//Return the list of Digits
//Return the list of digits
return listOfDigits;
}
public Long getSumOfList(){
Long sum = 0L; //Start the sum at 0 so you can add to it
private long getSumOfList(){
long sum = 0L; //Start the sum at 0 so you can add to it
//Add every number in the ArrayList to the sum
for(Long num : sumOfFifthNumbers){
for(long num : sumOfFifthNumbers){
sum += num;
}
//Return the sum
@@ -68,17 +68,17 @@ public class Problem30 extends Problem{
timer.start();
//Start with the lowest number and increment until you reach the largest number
for(Long currentNum = BOTTOM_NUM;currentNum <= TOP_NUM;++currentNum){
for(long currentNum = BOTTOM_NUM;currentNum <= TOP_NUM;++currentNum){
//Get the digits of the number
ArrayList<Long> digits = getDigits(currentNum);
//Get the sum of the powers
Long sumOfPowers = 0L;
for(Long num : digits){
long sumOfPowers = 0L;
for(long num : digits){
sumOfPowers += Math.round(Math.pow(num, POWER_RAISED));
}
//Check if the sum of the powers is the same as the number
//If it is add it to the list, otherwise continue to the next number
if(sumOfPowers.equals(currentNum)){
if(sumOfPowers == currentNum){
sumOfFifthNumbers.add(currentNum);
}
}
@@ -93,5 +93,5 @@ public class Problem30 extends Problem{
/* Results:
The sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839
It took 307.629 milliseconds to solve this problem.
It took 227.085 milliseconds to solve this problem.
*/