mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
107 lines
3.5 KiB
Java
107 lines
3.5 KiB
Java
//ProjectEuler/Java/Problem21.java
|
|
//Matthew Ellison
|
|
// Created: 03-18-19
|
|
//Modified: 03-28-19
|
|
//Evaluate the sum of all the amicable numbers under 10000
|
|
//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 mattrixwv.Algorithms;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
|
|
|
|
public class Problem21{
|
|
private static final Integer LIMIT = 10000;
|
|
public static void main(String[] argv){
|
|
//Setup the timer
|
|
Stopwatch timer = new Stopwatch();
|
|
|
|
//Setup the variables
|
|
ArrayList<Integer> divisorSum = new ArrayList<Integer>(); //Holds the sum of the divisors of the subscript number
|
|
divisorSum.ensureCapacity(LIMIT); //Reserving it now makes it faster later
|
|
//Make sure the arraylist is filled with 0's
|
|
for(int cnt = 0;divisorSum.size() < LIMIT;++cnt){
|
|
divisorSum.add(0);
|
|
}
|
|
|
|
//Start the timer
|
|
timer.start();
|
|
|
|
//Generate the divisors of all numbers < 10000, get their sum, and add it to the list
|
|
for(int cnt = 1;cnt < LIMIT;++cnt){
|
|
ArrayList<Integer> divisors = Algorithms.getDivisors(cnt); //Get all the divisors of a number
|
|
if(divisors.size() > 1){
|
|
divisors.remove(divisors.get(divisors.size() - 1)); //Remove the last entry because it will be the number itself
|
|
}
|
|
//System.out.printf("getSum(divisors) = %d\n", Algorithms.getSum(divisors));
|
|
divisorSum.set(cnt, Algorithms.getSum(divisors)); //Add the sum of the divisors of the vector
|
|
}
|
|
//Check every sum of divisors in the list for a matching sum
|
|
ArrayList<Integer> amicable = new ArrayList<Integer>();
|
|
for(Integer cnt = 1;cnt < divisorSum.size();++cnt){
|
|
Integer sum = divisorSum.get(cnt);
|
|
//If the sum is greater than the number of divisors then it is impossible to be amicable. Skip the number and continue
|
|
if(sum >= divisorSum.size()){
|
|
continue;
|
|
}
|
|
//We know that divisorSum.at(cnt) == sum, so if divisorSum.at(sum) == cnt we found an amicable number
|
|
if(divisorSum.get(sum).compareTo(cnt) == 0){
|
|
//A number can't be amicable with itself
|
|
if(sum.compareTo(cnt) == 0){
|
|
continue;
|
|
}
|
|
//Add it to the arraylist of amicable numbers
|
|
amicable.add(cnt);
|
|
}
|
|
}
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//Sort the arraylist for neatness
|
|
Collections.sort(amicable);
|
|
|
|
//Print the results
|
|
System.out.printf("All amicable numbers less than %d are\n", LIMIT);
|
|
for(int cnt = 0;cnt < amicable.size();++cnt){
|
|
System.out.println(amicable.get(cnt).toString());
|
|
}
|
|
System.out.printf("The sum of all of these amicable numbers is %d\n", Algorithms.getSum(amicable));
|
|
System.out.println("It took " + timer.getStr() + " to run this algorithm");
|
|
}
|
|
}
|
|
|
|
/* Results:
|
|
All amicable numbers less than 10000 are
|
|
220
|
|
284
|
|
1184
|
|
1210
|
|
2620
|
|
2924
|
|
5020
|
|
5564
|
|
6232
|
|
6368
|
|
The sum of all of these amicable numbers is 31626
|
|
It took 27.645 milliseconds to run this algorithm
|
|
*/
|