mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
92 lines
3.6 KiB
Java
92 lines
3.6 KiB
Java
//ProjectEuler/Java/Problem30.java
|
|
//Matthew
|
|
// Created: 10-27-19
|
|
//Modified: 10-27-19
|
|
//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
|
|
|
|
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 <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
|
|
import mattrixwv.Stopwatch;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
public class Problem30{
|
|
private static final Long TOP_NUM = 1000000L; //This is the largest number that will be checked
|
|
private static final Long BOTTOM_NUM = 2L; //Starts with 2 because 0 and 1 don't count
|
|
private static final Long POWER_RAISED = 5L; //This is the power that the digits are raised to
|
|
private static ArrayList<Long> sumOfFifthNumbers; //This is an ArrayList of the numbers that are the sum of the fifth power of their digits
|
|
//Returns an ArrayList with the individual digits of the number passed to it
|
|
private static 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();
|
|
//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){
|
|
listOfDigits.add(Long.valueOf(digits.substring(cnt, cnt + 1)));
|
|
}
|
|
//Return the list of Digits
|
|
return listOfDigits;
|
|
}
|
|
public static 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){
|
|
sum += num;
|
|
}
|
|
//Return the sum
|
|
return sum;
|
|
}
|
|
public static void main(String[] args){
|
|
Stopwatch timer = new Stopwatch();
|
|
sumOfFifthNumbers = new ArrayList<Long>();
|
|
|
|
//Start the timer
|
|
timer.start();
|
|
|
|
//Start with the lowest number and increment until you reach the largest number
|
|
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){
|
|
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)){
|
|
sumOfFifthNumbers.add(currentNum);
|
|
}
|
|
}
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//Print the results
|
|
System.out.printf("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is %d\n", getSumOfList());
|
|
System.out.println("It took " + timer.getStr() + " to run this algorithm");
|
|
}
|
|
}
|
|
|
|
/* 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 478.387 milliseconds to run this algorithm
|
|
*/
|